2013-04-30 127 views
-5

我是PHP的初學者,所以如果答案很明顯,我很抱歉。foreach in form not working properly

基本上,我希望用戶點擊提交按鈕,並將ID發佈到aaa.php。

<form action='aaa.php' method='get'>` 
<?php foreach ($a as $b) { 
?> 
    <tr> 
    <td><input type="submit" name="Action" value='View' /></td> 
    <td><?php echo $b['info'] ?></td> 
    <input type="hidden" name="id" value="<?php echo $b['id']"?> 
<?php echo } 
echo "</tr></form></table>"; 
?> 

使用上面的腳本,它似乎是foreach函數使每個id傳輸。即當使用method="get"時,我得到的網址爲"aaa.php?mailAction=View&id=2&id=1..."

我應該如何解決問題?

+0

它可以幫助你貼得更代碼,甚至讓我們知道'$ mail'和值'$了' – Dom 2013-04-30 08:41:57

+1

當您從表單提交時,該表單中的所有輸入都會發布。 – fullybaked 2013-04-30 08:42:35

+0

你的'$ a'變量包含什麼?你爲什麼使用foreach? – 2013-04-30 08:42:59

回答

0

你的foreach循環似乎如果你使用method=get在瀏覽器中查詢得到id=2&id=1正常工作。我認爲你必須首先理解HTML表單才能在這裏實現你的問題。

與您的代碼上面要生成形式一組ID:

<form action='aaa.php' method='get'> 
    <input type="hidden" name="id" value="2"> 
    <input type="hidden" name="id" value="1"> 

    <input type="submit" name="Action" value='View'/> 
</form> 

但實際上你想在同一時間發送一個ID。所以你必須產生多種形式來達到這個目的:

<form action='aaa.php' method='get'> 
    <input type="hidden" name="id" value="2"> 

    <input type="submit" name="Action" value='View'/> 
</form> 

<form action='aaa.php' method='get'> 
    <input type="hidden" name="id" value="1"> 

    <input type="submit" name="Action" value='View'/> 
</form> 

我希望你現在明白了。

爲了解決你的問題,你的PHP代碼應該是像這樣:

foreach ($a as $b) 
{ 
    echo "<form action='aaa.php' method='get'> 
      <input type='hidden' name='id' value=" . $b['id'] . "> 
      <input type='submit' name='Action' value='View'/> 
     </form>"; 
} 
+0

它的工作原理!非常感謝!! – user2335065 2013-04-30 09:04:43

+0

不客氣! :) – Alexxus 2013-04-30 09:09:54

0

試試這個

<table> 
    <form action="aaa.php" method="get"> 
    <?php foreach ($a as $b) : ?> 
     <tr> 
      <td><input type="submit" name="Action" value="<?php echo $b['id'];?>" /></td> 
      <td><?php echo $mail['info'] ?></td>    
     </tr> 
    <?php endforeach;?> 
    </form> 
</table> 

通過將ID上提交按鈕,你應該得到的URL

+0

謝謝!但實際上我在窗體上有多個按鈕,所以value ='View'是必需的。這是我第一次發佈在stackoverflow上,也許我應該更清楚,當我發佈它。但我真的沒有期待這麼快速的迴應!欣賞它! – user2335065 2013-04-30 08:52:10

0

點擊的按鈕的價值如何把每個ID在自己的形式?

<?php foreach ($a as $b) { 
?> 
<form action='aaa.php' method='get'> 
    <table> 
     <tr> 
     <td><input type="submit" name="Action" value='View' /></td> 
     <td><?php echo $mail['info'] ?></td> 
     ... 
     <input type="hidden" name="id" value="<?php echo $b['id']?>" /> 
     <tr> 
    </table> 
</form> 
<?php 
} 
?> 
0

你必須使用數組這種類型

<form action='aaa.php' method='get'>` 
<?php foreach ($a as $b) { 
?> 
    <tr> 
    <td><input type="submit" name="Action[]" value='View' /></td> 
    <td><?php echo $b['info'] ?></td> 
    <input type="hidden" name="id[]" value="<?php echo $b['id']"?> 
<?php echo } 
echo "</tr></form></table>"; 
?>