tags:

views:

50

answers:

1

Hello

I am using the following to generate a form:

<form method="post" action="">
<input type="hidden" name="group_name" value="<?php echo $user_group ?>" />
<table width="100%" border="0">
  <tr>
    <td>User</td>
    <td>Email</td>
    <td>&nbsp;</td>
  </tr>

<?php foreach ($user_info as $key => $array) {
    while ($row = mysql_fetch_array($array)) { ?>

    <input type="hidden" name="remove_user_id" value="<?php echo $row['id'] ?>" >
    <input type="hidden" name="email" value="<?php echo $row['email'] ?>" >

  <tr>
    <td><?php echo $row['first_name']." ".$row['last_name']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td><input type="submit" value="Remove" name="removeuser" /></td>
  </tr>

<?php } } ?>

</table>

...other form controls...

This is generating the expected form data. However, when a form row is posted to my processing system, it always sends the last row generated and I don't see why. Anyone see a problem with this?

+4  A: 

You have multiple submit buttons in the same form, and thus the last row is the last row to be processed and is the one sent to the server.

You need a different form for each row:

<input type="hidden" name="group_name" value="<?php echo $user_group ?>" />
<table width="100%" border="0">
  <tr>
    <td>User</td>
    <td>Email</td>
    <td>&nbsp;</td>
  </tr>

<?php foreach ($user_info as $key => $array) {
    while ($row = mysql_fetch_array($array)) { ?>

<form method="post" action="">
    <input type="hidden" name="remove_user_id" value="<?php echo $row['id'] ?>" >
    <input type="hidden" name="email" value="<?php echo $row['email'] ?>" >

  <tr>
    <td><?php echo $row['first_name']." ".$row['last_name']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td><input type="submit" value="Remove" name="removeuser" /></td>
  </tr>
</form>
<?php } } ?>

</table>

Notice the <form> element is now printed on each row. I'm not sure what you're doing with the other form elements, so those will probably need their own (new) <form> tag.

Also note, this is, without checking, incorrect HTML, so if you're worried about that, you'll need to go about this another way, but since you're using tables for layout, I don't think it'll be that big of a deal.

Edit:

Here's a better answer:

<table width="100%" border="0">
  <tr>
    <td>User</td>
    <td>Email</td>
    <td>&nbsp;</td>
  </tr>

<?php foreach ($user_info as $key => $array) {
    while ($row = mysql_fetch_array($array)) { ?>

  <tr>
    <td><?php echo $row['first_name']." ".$row['last_name']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td>
      <form method="post" action="">
        <input type="hidden" name="remove_user_id" value="<?php echo $row['id'] ?>" >
        <input type="hidden" name="email" value="<?php echo $row['email'] ?>" >
        <input type="hidden" name="group_name" value="<?php echo $user_group ?>" />
        <input type="submit" value="Remove" name="removeuser" />
      </form>
    </td>
  </tr>
</form>
<?php } } ?>

</table>

This should now do exactly what you want, should validate as correct HTML, but the other form elements will need a new <form> tag (the ones below the given code).

HalfBrian
Thank you. Now working! I often use this technique for admin changes to large amounts of data. I guess I missed a step.
YsoL8
Just for the record, I updated my answer with a more "correct" form.
HalfBrian
Wouldn't that break the form elements out of the grid? (I normally use CSS, but sometimes not using tables is daft IMO)
YsoL8
The form elements should still be visible within the table, aligned with the other elements, because they are within a `<td>` tag.
HalfBrian