John Davidson

javascript - How to store the input value to php variable

0 comments
Message:


<input type="text" id="transaction_id_input" name="transaction_name">

Above is the value that I want to store in PHP variables so I can compare it in the MySQL data to display specific data. I try storing the input value also to a javascript document.getElementByID("sampleID").value then store it in a variable but it didn't also work, hoping you will help me solve this problem.


<form method="GET" id="reg" name="valid" action="<?php echo htmlspecialchars(
$_SERVER["PHP_SELF"]
); ?>">
<input type="text" id="transaction_id_input" name="transaction_name">
<div class="wrapper" >
<div class="search-input">
<a href="" target="_blank" hidden></a>
<input type="text" id="searchmodal" placeholder="Type to search..">
<div class="icon"><i class="fas fa-search"></i></div>
</div>
</div>
<br><br>
<div class="card-header" id="cardheader2">
List of Medicine Transaction
</div><br><br>
<div class="enrollbutton2">
<a class="nav-link" id="transactioninfo_to_addtransan_class" data-toggle="modal" data-target="#myModal_transaction">Add Transaction
<span>
<lord-icon
src="https://cdn.lordicon.com/mecwbjnp.json"
trigger="loop"
colors="primary:#ffffff,secondary:#ffffff"
stroke="70"
style="width:30px;height:30px">
</lord-icon>
</span>
</a>
</div>
<div class="tableholdermodal" id="transactiontable">
<br><br><br><br>
<table>
<thead>
<tr>
<th scope="col">Transaction No.</th>
<th scope="col">Medicine Code</th>
<th scope="col">Supply</th>
<th scope="col">Lot No.</th>
<th scope="col">Expiration Date</th>
<th scope="col">Inputed Date</th>
</tr>
</thead>
<tbody id="myModalTable">

<?php
include "pdo_connection2.php";
$stm3 = $conn->query(
"SELECT * FROM tbl_transaction WHERE transaction_medicinecode = '' "
);
$rows = $stm3->fetchAll(PDO::FETCH_ASSOC);
if (count($rows) > 0) {
foreach ($rows as $row) { ?>
<tr>
<td data-label='Transaction ID' value="<?php echo $row[
"transaction_id"
]; ?>"><?php echo $row["transaction_id"]; ?></td>
<td data-label='Medicine Code' value="<?php echo $row[
"transaction_medicinecode"
]; ?>"><?php echo $row["transaction_medicinecode"]; ?></td>
<td data-label='Supply' value="<?php echo $row[
"transaction_supply"
]; ?>"><?php echo $row["transaction_supply"]; ?></td>
<td data-label='Lot No.' value="<?php echo $row[
"transaction_lotnum"
]; ?>"><?php echo $row["transaction_lotnum"]; ?></td>
<td data-label='Expiration Date' value="<?php echo $row[
"transaction_expdate"
]; ?>"><?php echo $row["transaction_expdate"]; ?></td>
<td data-label='Date Registered' value="<?php echo $row[
"transaction_date"
]; ?>"><?php echo $row["transaction_date"]; ?></td>
<td data-label='Action'>
<button type="button" class="transaction_update_class" name="transaction_update_name" data-toggle="modal" data-target="#myModalupdate_transactopn" value="<?php echo $row[
"transaction_id"
]; ?>">
<lord-icon
src="https://cdn.lordicon.com/wloilxuq.json"
trigger="loop"
colors="primary:#fb0054,secondary:#f55b2a "
stroke="70"
style="width:55px;height:55px">
</lord-icon>
</button>

<button type="button" class="transaction_delete_class" name="transaction_delete_name" data-toggle="modal" data-target="#myModaldelete_transaction" value="<?php echo $row[
"transaction_id"
]; ?>">
<lord-icon
src="https://cdn.lordicon.com/gsqxdxog.json"
trigger="loop"
colors="primary:#fb0054,secondary:#f55b2a "
stroke="70"
style="width:55px;height:55px">
</lord-icon>
</button>

</td>
</tr>

<?php }
} else {
?>
<tr><td><h4 id="h4nodatafound">------ NO DATA FOUND ------</h4></td></tr>
<?php
}
?>


</tbody>
</table>

</div>
</form>


My Answer:
To store the input value in a PHP variable, you can use JavaScript to get the input value and then send it to the server using AJAX. Here's an example of how you can achieve this:

1. Add an event listener to the input field to capture the value when it changes:

javascript
document.getElementById("transaction_id_input").addEventListener("input", function() {
var inputValue = this.value;
// Send the input value to the server using AJAX
var xhr = new XMLHttpRequest();
xhr.open("POST", "store_input.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("inputValue=" + inputValue);
});


2. Create a PHP file (store_input.php) on the server to receive the input value and store it in a PHP variable:

php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputValue = $_POST["inputValue"];
// Now you can use $inputValue in your PHP code to compare with MySQL data
}
?>


By following these steps, you can capture the input value in JavaScript and store it in a PHP variable for further processing.

Rate this post

4 of 5 based on 3617 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us