简体   繁体   English

PHP在while循环中提交表单隐藏的输入值

[英]php submit form hidden input values in a while loop

I have had this issue a number of times so I wonder if others have it too or anyone knows a good solution for it. 我已经多次遇到过这个问题,所以我想知道是否其他人也有此问题,或者有人知道它有很好的解决方案。

 $i = 1;
 <form method="POST" action="submit.php">
 while($i < 10) {
 <p> info </p>
 <input type="hidden" value="$i" name="number" />
 <input type="submit" value="accept" />
 <input type="submit" value="decline" />
 $i++;
 }
 </form>

The problem I have is that it always submits the highest value. 我的问题是,它始终提交最高价值。 For this example I will have 10 submit and decline buttons and want to determine if they click on the 4th accept button that the server side language will recognize it as the 4th submit button. 对于此示例,我将有10个提交和拒绝按钮,并希望确定它们是否单击了第4个接受按钮,服务器端语言会将其识别为第4个提交按钮。 I have added a variable to the input hidden name = number$i and it works but sometimes I have a loop of 50-100 and it seems like there is a better way to collect the data with a different variable name for that many options. 我在输入的隐藏名称= number $ i中添加了一个变量,它可以工作,但是有时我有一个50-100的循环,并且似乎有更好的方法来收集具有这么多选项的不同变量名的数据。

Any help would be great. 任何帮助都会很棒。 Thanks. 谢谢。

The problem I have is that it always submits the highest value. 我的问题是,它始终提交最高价值。

Nope. 不。 All values are submitted – but PHP overwrites parameters with the same name. 所有值均已提交-但PHP会覆盖具有相同名称的参数。

You could use a name like number[] – the square brackets will make PHP generate an array of the submitted values, rather than overwriting them. 您可以使用诸如number[]类的名称-方括号将使PHP生成提交值的数组,而不是覆盖它们。

But this won't help you here, since your submit buttons will still submit the whole form – so you will get all the values, no matter what submit button was pressed. 但这对您无济于事,因为您的提交按钮仍将提交整个表单,因此无论按下什么提交按钮,您都将获得所有值。

Either you use individual forms for each item, or you will have to pass the info with the submit buttons themselves. 您要么为每个项目使用单独的表单,要么必须自己使用“提交”按钮传递信息。 Of course you don't want to put that info into the value, because that's the buttons text – but you could put it into the name, in the form of accept[37] – then you'll get an array with the key 37, so you would only have to see which key is used in the array received. 当然,您不想将该信息放入值中,因为这是按钮文本–但是您可以将其以accept[37]的形式放入名称中,然后将得到带有 37的数组,因此您只需要查看在接收到的数组中使用了哪个键。

But if you don't want to have to submit the form over and over again for each single item – then maybe you should rather use radio buttons for the accept/decline choices, and then submit them all at once. 但是,如果您不想为每个项目一遍又一遍地提交表单,那么也许您应该使用单选按钮作为接受/拒绝选项,然后一次提交所有表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM