如果使用按钮元素提交,有没有办法识别表单?
我已经开始使用按钮元素来提交我的表单
<button type="submit">Send</button>
我知道如果我使用像这样的输入元素,我可以轻松地在 PHP 中确定提交了哪个表单(通过读取 $_POST['form']< 的值/code> 在下面的示例中)。
<input type="submit" value="Send" name="form" value="contact" />
有没有办法使用按钮元素来做到这一点?每个表单将有一个按钮元素。我知道 IE6 在一种表单中存在多个按钮元素的问题,并且 IE7 发送innerHTML而不是值。
这会可靠吗?
HTML
<button type="submit" name="form-contact" value="true">Send</button>
PHP
if (isset($_POST['form-contact'])) {
// Contact Form submitted
}
非常感谢
I've started using the button element to submit my forms
<button type="submit">Send</button>
I know if I used an input element like so, I could easily determine in PHP which form was submitted (by reading the value of $_POST['form']
in the example below).
<input type="submit" value="Send" name="form" value="contact" />
Is there a way to do this using the button element? There will be one button element per form. I know that IE6 has problems with multiple button elements in one form, and IE7 sends the innerHTML instead of the value.
Would this be reliable?
HTML
<button type="submit" name="form-contact" value="true">Send</button>
PHP
if (isset($_POST['form-contact'])) {
// Contact Form submitted
}
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是对您问题的直接答案,但您始终可以使用隐藏输入来区分形式:
另一种形式:
This is not a direct answer to you question, but you can always use hidden inputs to distinguish the forms:
Another form:
如果处理表单的脚本需要区分不同的表单,则应该使用隐藏输入,因为它是可靠的跨浏览器。测试提交按钮的名称/值不太可靠,因为如果用户通过回车键提交表单,而另一个表单元素具有焦点,则某些浏览器根本不会发送提交按钮的名称/值对。在这种常见情况下失败的表单处理器确实很蹩脚。按钮、输入类型=按钮和输入类型=图像都存在跨浏览器不一致的情况。
实际上,您甚至很少需要隐藏的输入。您可以为不同的表单使用不同的输入名称。例如 name=contact_email、name=order_email 等等,然后测试哪一个存在以确定表单身份。但是,这会变得更加复杂,我个人认为当您可以将隐藏的输入放在那里时,不值得付出额外的努力。
If the script which processes the form needs to distinguish between different forms, you should use a hidden input, because it's reliable cross browser. Testing for the name/val of an submit button isn't very reliable because some browsers simply won't send the name/value pair of the submit button if the user submits the form via the enter key while a different form element has focus. Form processors that fail in this common scenario are really lame. button, input type=button, and input type=image all have cross browser inconsistencies.
You rarely even need the hidden input actually. You can just use different input names for different forms. eg name=contact_email, name=order_email etc.. and then just test which one exists to determine form identity. But, this gets more complicated and I personally don't feel it's worth the extra effort when you can just put the hidden input there.
您可以使用 AJAX 发送与单击的按钮相对应的 form_name,只需将表单名称嵌入到 url 中
you can use AJAX to send form_name corresponding to clicked button, just embed the name of form along the url