简体   繁体   English

动态生成的提交按钮,单击了哪个?

[英]dynamically-generated submit buttons, which one was clicked?

Let's assume a page has a bunch of submit buttons that are generated by a PHP loop of some kind. 假设页面上有一堆由某种PHP循环生成的提交按钮。 They are named after the loop value, so the result would look something like this: 它们以循环值命名,因此结果看起来像这样:

<input type="submit" name="0" id="0" value="click me">
<input type="submit" name="1" id="1" value="click me">

etc. 等等

Let's say that there could be anywhere between zero and a gazillion of these buttons. 假设这些按钮可能介于零到一千亿个之间。 Assuming that the form is POST, how would I identify, on the page that loads afterward, which of the buttons had been clicked? 假设表单是POST,我如何在随后加载的页面上识别单击了哪个按钮?

As I mentioned in the comments above, you would be better to use an array in the HTML: 正如我在上面的评论中提到的,您最好在HTML中使用数组:

<form action="" method="post">
<?php
for ($i = 0; $i < 100 ; $i++) {
    echo '<input type="submit" name="clicked['.$i.']" value="clicked" />';
}
?>
</form>

Then in your PHP you can get the clicked input by doing: 然后在您的PHP中,您可以通过执行以下操作获得单击的输入:

echo key($_POST['clicked']); // Prints, for example, 28

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

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