I have a table
that is in a foreach
loop, and every iteration the hidden input
receives the ID from the database. My table has in each column an input
field because I'd like to edit the data from the database right in the table, and to make it happen I had to put a submit button
that when clicked retrieves the ID from the hidden input
into another file that then does the operation.
<form action="../php/Operations.php" method="post">
//...
<?php foreach ($Service->select() as $col) { ?>
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
//...
The problem is that the $_POST
value is always the very first from the table, because there are n hidden input
with the same name.
I'd like to know if there is a way to retrieve the hidden input
's value from the clicked row, having in mind I'm not using $_GET
, but a submit button
,
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
that when clicked is supose to execute the code I wrote:
switch($_REQUEST["submit"]) {
case "update":
$ServiceDatabase->update($_POST["id"]);
break;
//...
Thank you.
If you want multiple inputs with the same name use
name="id[]"
for the input name attribute.$_POST
will then contain an array for name with all values from the input elements. Then you can then loop over this array.Example:
PHP
EDIT: You can change your code like this so each submit button is tied to exactly one hidden input with the
name="id"
.Rendered HTML: