John Davidson

html - Click to insert value into a input of a PHP form

0 comments
Message:


I have created a table with a mix of SQL, HTML and PHP.
See code:


<table>
<tr>
<th>Activated by:</th>
<th>Serialnumber:</th>
<th>Timestamp:</th>
<th>Total consumed energy in kWh:</th>
<th>Total bought enery in kWh:</th>
<th>Energy left in kWh:</th>
</tr>
<?php

if ($serialresult) {
while ($serialrow = mysqli_fetch_assoc($serialresult)) {
$serialnumbers = $serialrow['serial_number'];

$sql = "SELECT * FROM Smartmeterlist WHERE serial_number='$serialnumbers'";
$statsresult = mysqli_query($connection, $sql);
if ($statsresult) {
while ($statsrow1 = mysqli_fetch_assoc($statsresult)) {
$sql = "SELECT * FROM timeframe WHERE times = (SELECT MAX(times) FROM timeframe WHERE serial_number='$serialnumbers')";
$statsresult = mysqli_query($connection, $sql);
if ($statsresult) {
while ($statsrow = mysqli_fetch_assoc($statsresult)) {
echo "<tr><td>" . $statsrow1['activated_by'] . "</td> <td>" . $serialnumbers . "</td><td>" . $statsrow['times'] . "</td><td>" . $statsrow['consumed_energy'] . "</td><td>" . $statsrow['bought_energy'] . "</td><td>" . $statsrow['diff_energy'] . "</td></tr>";
}
}
}
}



}
}

}
?>


Below I create a form to execute an action:


        <div class="page-content">
<form action="includes/distributeenergy.inc.php" method="POST">
<input type="Number" step="0.01" name="serial_number"
placeholder="Serial number"> <br> <input type="Number" step="0.01"
name="distribute_energy" placeholder="Assign your energy"> <br>
<div class="row">
<input class="btn" type="submit" name="DE-btn" id="DE-btn"
value="Distribute">
</div>
</form>
</div>

In the table are so called serial numbers of electronic devices.
I want to be able to click on a serial number (hyperlink or similar) and the serial number is used directly as an input for the form. So it is inserted into the "search bar".


Is there a simple way to realize this? Thanks a lot



My Answer:
Yes, you can achieve this by adding a link or button next to each serial number in the table that will populate the input field in the form with the clicked serial number. Here's an example of how you can do this:

1. Modify the table row in your PHP code to include a link or button next to the serial number:

php
echo "" . $statsrow1['activated_by'] . " " . $serialnumbers . "" . $statsrow['times'] . "" . $statsrow['consumed_energy'] . "" . $statsrow['bought_energy'] . "" . $statsrow['diff_energy'] . "";


2. Add some JavaScript to handle the click event on the link and populate the input field in the form:

html



With this code, when you click on a serial number in the table, it will populate the input field in the form with that serial number. You can style the link to look like a button or any other element as needed.

Rate this post

5 of 5 based on 2434 votes

Comments




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