0

This foreach loop creates about 120 table rows.

In every row there are two input fields where the user can update/change the first and last name.

After changing the input value, the user clicks on the "Save" button to send the input values with $_POST.

    <table>
    <? foreach ($members as $member) {?>
    <form name="member" action="" method="POST">
    <tr>
    <td><input name="id" type="hidden" value="<? echo $member['id'] ?>"></td>
    <td><input name="first_name" type="text" value="<? echo $member['first_name'] ?>"></td>
    <td><input name="last_name" type="hidden" value="<? echo $member['last_name'] ?>"></td>
    <td><input type="submit" value="Save"></td>
    </tr>
    </form>
<? } ?>

I see the problem, that there are 120 input fields with name="id" and name="first_name" and name="last_name".

Normaly I would take the submited value like this and but it into a variable:

$first_name = $_POST['first_name'];

Will the form only submit the values inside the <form>-tags where the submit button is located or is there another solution for preventing that values get mixed up between other forms?

Mike
  • 3,200
  • 5
  • 22
  • 33

2 Answers2

3

Since your form tag is inside of your foreach loop, then yes: each submit button will only submit the first_name and last_name values that are on the same form as the clicked submit button.

However, your html is not valid. Valid html would dictate that you either create each form within a table cell (td tag), or find a different way to correlate a save button with specific fields (with some javascript or different markup). See this answer: https://stackoverflow.com/a/1249715/2061789

Community
  • 1
  • 1
Divey
  • 1,699
  • 1
  • 12
  • 22
  • thank you! and what would be a solution when the form is outside the foreach loop? any tutorials? thank you! – Mike Oct 16 '13 at 19:12
  • If the form tags are outside for each loop, the form would contain `` elements with duplicate names : `first_name`, `last_name`. First of all, using duplicate `"name"` attributes would be plain wrong, and second, the form would misbehave : it would not submit the right values for the inputs. – Sasanka Panguluri Oct 16 '13 at 19:15
  • @SasankaPanguluri thank you - but how can somebody handle this when the form would be outside the `form`-tag? – Mike Oct 16 '13 at 19:17
  • @Divey - but to create a new teable in the `td` tag like in the your linked answer will not work when there are also `td` tags? – Mike Oct 16 '13 at 19:24
  • @Mike It is complicated. However, here's how: Keep a `hidden input` field to keep a track of number of `"first name"` and `"last name"` fields in the form. Let's call that number `$count`. And append each name tag with a number. Inside the loop, just say `"first_name$count"` and same for the last name as well. Initialize `$count` outside the loop. And before the `}`, increment `$count`. – Sasanka Panguluri Oct 16 '13 at 19:53
0

You will need to use different table for each form. But you can distinguish the fields, by keeping different names, based on a counter. Like this:

<?
$i = 0;
foreach ($members as $member) {
?>
<table>
<form name="member" action="" method="POST">
<tr>
<td><input name="<?php echo 'id_'.$i;?>" type="hidden" value="<? echo $member['id'] ?>"></td>
<td><input name="<?php echo 'first_name_'.$i;?>" type="text" value="<? echo $member['first_name'] ?>"></td>
<td><input name="<?php echo 'last_name_'.$i;?>" type="hidden" value="<? echo $member['last_name'] ?>"></td>
<td><input type="submit" value="Save"></td>
</tr>
</form>
</table>
<?php
$i++;
} ?>
Aris
  • 4,643
  • 1
  • 41
  • 38
  • td is inside each table. you will read the variable from the input name. – Aris Oct 17 '13 at 12:59
  • sorry I mean `th`: and how would the table structure look like when the table contains also as first row `th` tags? because this row shouldn't be in the `foreach` loop – Mike Oct 17 '13 at 13:24