[英]How to store POST values for each form in an array?
基本上,該代碼應該很簡單,但卻無法正常工作! 第一頁:
<?php
$i = 1;
$var;
while($i != 10)
{
$var="
<form id='upload' action='test2.php' method='POST'>
<input type='hidden' name='test' value='{$i}'>
<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div> ";
$i = $i+1;
echo $var;
}
?>
第二頁:
<?php
echo $_POST['test'];
?>
當我運行代碼時,我總是只得到最后一個值(9)...每個按鈕都需要具有不同的值。
創建具有多個input
元素名稱的單個form
作為數組,以在單個輸入中獲取多個值
嘗試這個:
<input type='hidden' name='test[]' value='{$i}'>
現在您將收到一個test
數組,其值為$_POST['test']
具有不同的值
另一個建議的解決方案的問題是您將有10個表單,但是您將無法一次提交所有項目。 您將只能提交其中之一的值。
我相信您正在嘗試創建10個輸入元素,而不是10個單獨的表單:
<?php
$i = 1;
$var;
$var .= "<form id='upload' action='test2.php' method='POST'>"
while($i != 10)
{
$var .= "<input type='hidden' name='test[]' value='{$i}'>"
$i = $i+1;
}
$var .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div>"
echo $var
?>
以下是我建議的代碼,而不是您建議的代碼:
<?php
$html = "<form id='upload' action='test2.php' method='POST'>";
for ($i = 1; $i <= 10; $i++){
$html .= "<input type='hidden' name='test[]' value='{$i}'>";
}
$html .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>"
$html .= "</form>";
echo $html
?>
您不需要多種形式或隱藏的輸入即可實現此目的。 您可以只使用按鈕,並將它們的值設置為$i
。
echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';
在test2.php
, $_POST['test']
將具有您單擊的按鈕的$i
值。
比Mahesh的答案需要做的工作還多
首先,是一個問題:您真的要10個表格嗎?還是想要一個包含10個問題的表格? 請記住, $_POST
將僅包含提交表單中的值(閱讀:一種表單)。
我想你想要類似的東西
<form id="upload" action="test2.php" method="POST">
<?php for ($i = 0; $i < 10; $i++) { ?>
<input name="test[]" type="hidden" value="<?=$i?>">
<?php } ?>
<button type="submit">submit</button>
</form>
編輯:鑒於以下您的回答,為什么不使用查詢參數?
<a href="test2.php?page=<?=$i?>">Go to Album <?=$i?></a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.