$_post data not as expected
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> </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. Any开发者_StackOverflowone see a problem with this?
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> </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> </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).
精彩评论