I am trying to display a button next to items that are populated from a mysql database and do something when user clicks on them. What i have so far is :
if ($result->num_rows > 0) {
echo "<table><tr><th>SERIAL_NO</th><td></td><th>IP</th>
<td></td><td></td><th>MAC</th><td></td></tr>";
while($row = $result->fetch_assoc())
{
echo "<style> td {text-align:center;}</style>";
echo "<tr><td>".$row["serial_no"]."</td><td>"."|"."
</td><td>".$row["ip"]."</td><td></td><td>"."|"."</td>
<td>".$row["mac"]."</td><td>"."<input type=\"button\"
value=\"Rebuild\" onclick=\"rebuild()\" />"."</td>
</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
function rebuild()
{
echo "The rebuild function is called.";
}
However these button click events are not working. Can somebody please tell me what's the purpose of onclick=func()
in php and what is a good simple solution (I don't wanna make it too complicated, all I want is to pass the row index to a function when the button is pressed).
Update: The new code is :
if ($result->num_rows > 0) {
echo "<style> table, th, td {border: 1px solid black;}
</style>";
echo "<table><tr><th>SERIAL_NO</th><th>IP</th><th>MAC</th>
<th>Action</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<style> td {text-align:center;}</style>";
echo "<tr><td>".$row["serial_no"]."</td>
<td>".$row["ip"]." </td>
<td>".$mac."</td>
<td>".'<form>action="file_download.php" method="post">
<input type="submit" name="serial_no" value="' .
$serial_no . '"</input></form>' .
"</td></tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
With this I am just getting the literal html code on the browser, here is the exact text in these cells: action="file_download.php" method="post"> 10001
Note: 10001 is $serial_no for that row.
source https://stackoverflow.com/questions/67892020/handling-button-events-for-sql-items
No comments:
Post a Comment