How can I tell which button was clicked in a PHP form submit?
In HTML:
<input type="submit" id="btnSubmit" name="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" name="btnDelete" value="Delete" />
In PHP:
if (isset($_POST["btnSubmit"])){
// "Save Changes" clicked
} else if (isset($_POST["btnDelete"])){
// "Delete" clicked
}
With an HTML form like:
<input type="submit" name="btnSubmit" value="Save Changes">
<input type="submit" name="btnDelete" value="Delete">
(using <form method=POST
)
The PHP code to use would look like:
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:
- 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 Enter key is pressed.
Other ways to submit a form exist, and some browsers/versions decide not to send the name/value of any submit buttons in some of these situations. For example, many users submit forms by pressing the Enter key when the cursor/focus is on a text field. Forms can also be submitted via JavaScript, as well as some more obscure methods.
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!
Edit, here's more examples:
3+ button form:
<input type="submit" name="btnSubmit1" value="1">
<input type="submit" name="btnSubmit2" value="2">
<input type="submit" name="btnSubmit3" value="3">
php:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Something posted
if (isset($_POST['btnSubmit3'])) {
// btnSubmit3
} else if (isset($_POST['btnSubmit2'])) {
// btnSubmit2
} else {
// Assume btnSubmit1
}
}
Single button form:
<input type="submit" name="btnSubmit1" value="1">
php:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Something posted
// Assume btnSubmit1
}
Notice that in all cases, you can and should assume the first submit button to appear in the form's html was the button that was clicked, unless you can detect a different button. Only the buttons which appear later in the form should be explicitly tested for.
In other words, the first button is always assumed to be the form submitter, unless you can detect a different button as the submitter.
What about <form method="GET"
?
The reason we can use $_SERVER['REQUEST_METHOD'] === 'POST'
to detect a form POST is because POST is deliberate, while GET is not - GET is the default. So, using $_SERVER['REQUEST_METHOD'] === 'GET'
would be unreliable, because someone may just be loading the page/url, and not actually submitting a form, as the browser will use GET in either scenario because it is the default request method.
There's a variety of different ways to reliably detect a GET form submission, but a simple and reliable method is to just add <input type=hidden name=submitted value=1>
to the form, and then instead of using if ($_SERVER['REQUEST_METHOD'] === 'POST')
do if (isset($_GET['submitted']))
to detect form submission. The code detecting which button was pressed stays the same as it was for POST.
Browser Support:
This strategy has excellent browser support and does not rely on any browser specific behavior, nor any newer html5 features. It should work properly with both modern and ancient browsers, even from the early 2000's. Also, the php code logic is easily adapted to other languages because it does not rely on any tricky or php-specific behaviors.