如何判断在 PHP 表单提交中点击了哪个按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2680160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell which button was clicked in a PHP form submit?
提问by Sonny Boy
I have several buttons on my page, but I'm not sure how to tell which one was clicked. Here's the markup for my two buttons:
我的页面上有几个按钮,但我不知道如何判断点击了哪个按钮。这是我的两个按钮的标记:
<input type="submit" id="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" value="Delete" />
回答by goat
With an HTML form like:
使用 HTML 表单,例如:
<input type="submit" name="btnSubmit" value="Save Changes" />
<input type="submit" name="btnDelete" value="Delete" />
The PHP code to use would look like:
要使用的 PHP 代码如下所示:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Something posted
if (isset($_POST['btnDelete'])) {
// btnDelete
} else {
// Assume btnSubmit
}
}
You should always assume or default to the first submit button to appear in the form HTML source code. In practice, the various browsers reliably send the name/value of a submit button with the post data when:
您应该始终假定或默认第一个提交按钮出现在表单 HTML 源代码中。在实践中,各种浏览器在以下情况下可靠地发送带有发布数据的提交按钮的名称/值:
- The user literally clicks the submit button with the mouse or pointing device
- Or there is focus on the submit button (they tabbed to it), and then the Enterkey is pressed.
- 用户直接用鼠标或指点设备点击提交按钮
- 或者将焦点放在提交按钮上(他们用选项卡指向它),然后Enter按下该键。
Other ways to submit a form exist, and some browsers/versions decide not to send the name/value of any submit buttonsin some of these situations. For example, many userssubmit forms by pressing the Enterkey when the cursor/focus is on a text field. Forms can also be submitted via JavaScript, as well as some more obscure methods.
存在提交表单的其他方式,并且某些浏览器/版本决定在其中某些情况下不发送任何提交按钮的名称/值。例如,当光标/焦点位于文本字段上时,许多用户通过按Enter键来提交表单。表单也可以通过 JavaScript 以及一些更晦涩的方法提交。
It's important to pay attention to this detail, otherwise you can really frustrate your users when they submit a form, yet "nothing happens" and their data is lost, because your code failed to detect a form submission, because you did not anticipate the fact that the name/value of a submit button may not be sent with the post data.
注意这个细节很重要,否则当你的用户提交表单时你真的会让他们感到沮丧,但是“什么也没发生”并且他们的数据丢失了,因为你的代码没有检测到表单提交,因为你没有预料到这个事实提交按钮的名称/值可能不会与发布数据一起发送。
Also, the above advice should be used for forms with a single submit button too because you should always assume a default submit button.
此外,上述建议也应用于具有单个提交按钮的表单,因为您应该始终假设默认提交按钮。
I'm aware that the Internet is filled with tons of form-handler tutorials, and almost of all them do nothing more than check for the name and value of a submit button. But, they're just plain wrong!
我知道 Internet 上充斥着大量的表单处理程序教程,而且几乎所有教程都只是检查提交按钮的名称和值。但是,他们完全错了!
回答by MiffTheFox
In HTML:
在 HTML 中:
<input type="submit" id="btnSubmit" name="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" name="btnDelete" value="Delete" />
In PHP:
在 PHP 中:
if (isset($_POST["btnSubmit"])){
// "Save Changes" clicked
} else if (isset($_POST["btnDelete"])){
// "Delete" clicked
}
回答by Kavishka Hirushan
All you need to give the name attribute to the each button. And you need to address each button press from the PHP script. But be careful to give each button a unique name. Because the PHP script only take care of the name most of the time
您需要为每个按钮提供 name 属性。并且您需要从 PHP 脚本中解决每次按下按钮的问题。但是要小心为每个按钮指定一个唯一的名称。因为 PHP 脚本大部分时间只关心名称
<input type="submit" name="Submit_this" id="This" />
回答by Karthik
Are you asking in php or javascript.
你是在 php 还是 javascript 中问的。
If it is in php, give the name of that and use the post or get method, after that you can use the option of isset or that particular button name is checked to that value.
如果它在 php 中,请给出名称并使用 post 或 get 方法,之后您可以使用 isset 选项或将特定按钮名称检查为该值。
If it is in js, use getElementById for that
如果它在 js 中,请使用 getElementById

