2010-06-25 46 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