如何在 php 中为 $_POST['variable'] 添加增量值?

HOW to add increment value to $_POST['variable'] in php?

我正在使用动态表单,用户可以在其中为他想要的特定字段添加更多输入文本框,并且每个框的名称都会随着增量变化,例如:

<form method="post" action="somescript.php">
<input type="text" name="textbox" />
<input type="text" name="textbox1" />
<input type="text" name="textbox2" />
<input type="text" name="textbox3" />
.... and so on
</form>

我想在循环后回显这些数据:

<?PHP
  $k=$_POST['counter']; //counter value coming as post variable
  for($i=1$i<=$k;$k++){
    echo $_POST['textbox'.$i]; //something like this......?
  }
?>

请回复。

改为使用数组表示法。

<form method="post" action="somescript.php">
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox][" />
.... and so on
</form>

表单提交后,$_POST['textbox']会是一个数组,可以循环遍历:

foreach ($_POST['textbox'] as $textbox) {
    echo $textbox;
}

我刚刚遇到这个问题,因为我有需要动态创建的数据块并且

echo $_POST["textbox$i"];

在没有连接的情况下工作。让我知道这是否是不好的做法,但它适用于我的情况。数组方式对我不起作用。很抱歉在一个 3 年前的问题上发布这个。我不确定这是否是不好的做法。谢谢