2010-06-25 47 views
0

我使用下面的生成表单:

<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... 

这是产生预期的表单数据。但是,当表单行发布到我的处理系统时,它总是发送最后一行生成,我不明白为什么。任何人都看到这个问题?

回答

4

您有多个提交按钮的形式相同,因此最后一行是要处理的最后一行,并且是发送到服务器的那一行。

你需要一个不同的形式,为每一行:

<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> 

通知的<form>元素现在被印制在每一行。我不确定你对其他表单元素做了什么,因此这些可能需要自己的(新)<form>标记。

另外请注意,这是,没有检查,不正确的HTML,所以如果你担心这一点,你需要以另一种方式去做,但由于你使用表格布局,我没有认为这将是一笔交易。

编辑:

这里有一个更好的答案:

<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> 

这应该现在你想要什么,应该验证为正确的HTML,但其他形式的元素将需要一个新的<form>标签(给定代码下面的那些)。

+0

谢谢。工作中!我经常使用这种技术来管理更改大量数据。我想我错过了一步。 – YsoL8 2010-06-25 15:58:03

+0

为了记录,我用更“正确”的形式更新了答案。 – HalfBrian 2010-06-25 16:00:05

+0

这不会破坏网格中的表单元素吗? (我通常使用CSS,但是有时候不使用表格是daft IMO) – YsoL8 2010-06-25 16:10:41