PHP - Retrieve via $_POST the hidden input's value between several others

4k views Asked by At

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.

3

There are 3 answers

6
Chan On BEST ANSWER

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:

<form method="post">
    <input type="hidden" name="id[]" value="foo"/>
    <input type="hidden" name="id[]" value="bar"/>
    <input type="hidden" name="id[]" value="baz"/>
    <input type="submit" />
</form>

PHP

$ids= $_POST['id'];
print_r($ids);

EDIT: You can change your code like this so each submit button is tied to exactly one hidden input with the name="id".

<?php foreach ($Service->select() as $col) { ?>
    <form action="../php/Operations.php" method="post">
    <input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
    <button type="submit" name="submit" value="update" class="btn_action">Editar</button>
    </form>
<?php } ?>

Rendered HTML:

<form action="../php/Operations.php" method="post"><input type="hidden" value="1234" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="5678" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="9101112" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
4
Camden Narzt On

Either wrap each col in a separate form and have a separate submit button per col or make the name attribute an array. i.e. name="id[<?php echo htmlspecialchars($id); ?>"

0
jameshwart lopez On

i would recommend you to use ajax for submitting the data

Just create a click event in for the button and get the button id

in your htm form create an id attribute like this

<input type="hidden" id="colId<?php echo $col["id"]; ?>" value="<?php echo $col["id"]; ?>" />

and your button should look like this

<button class="buttonSubmit" id="<?php echo $col["id"]; ?>" >Submit</button>
$(".buttonSubmit").click(function(){
   var id = $(this).attr("id");
   var hiddeninput = $("colId"+id).val();
   // here you can process how would you send the data to a php file 
});