dongtaijiao7140 2013-11-14 18:01
浏览 57
已采纳

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

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. 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.

Any help would be great. Thanks.

  • 写回答

1条回答 默认 最新

  • dongliantong3229 2013-11-14 18:08
    关注

    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.

    You could use a name like number[] – the square brackets will make PHP generate an array of the submitted values, rather than overwriting them.

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用