我可以获取其他表单中提交按钮的名称吗?
我有一个有 3 个提交按钮的表单。它们的名称是在循环中生成和分配的。现在,如果我使用 post 方法,如何访问单击的提交按钮的名称。
以下是我的代码示例:
**one.php**
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
?>
<button type="submit" name="<?php echo $i ?>" value="<?php echo $i ?>" >
</button>
<?php
}
?>
</form>
**two.php**
{
code???????
}
也许我可以在 one.php 中使用按钮标记的 onsubmit 属性,但我无法获取输出。有什么建议吗?
I have a form which has 3 submit buttons. Their names are generated and assigned in a loop. Now if I use a post method, how can access the name of the submit button which was clicked.
The following is the example of my code:
**one.php**
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
?>
<button type="submit" name="<?php echo $i ?>" value="<?php echo $i ?>" >
</button>
<?php
}
?>
</form>
**two.php**
{
code???????
}
May be I can use onsubmit attribute for the button tag in one.php, but I am unable to get the output. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需检查
$_POST
是否有一个条目包含三个按钮中每个按钮的名称:Note that I'd suggest you give better names *(that don't begin with a number)* to your buttons -- which means generating your form like this :
并且,在提交表单时,使用如下所示的内容:
*BTW: your `while` loop's syntax is incorrect -- it seems you've mixed up [`while`][1] and [`for`][2] ;-)*
You could just check in
$_POST
if there is an entry with the name of each one of your three buttons :Note that I'd suggest you give better names *(that don't begin with a number)* to your buttons -- which means generating your form like this :
And, on form's submission, using something like this :
*BTW: your `while` loop's syntax is incorrect -- it seems you've mixed up [`while`][1] and [`for`][2] ;-)*
生成另一个输入元素
,然后使用
$_REQUEST['buttonId']
获取您的 idGenerate another input element
And then get your id with
$_REQUEST['buttonId']