How can I tell which button was clicked in a PHP form submit?

Php

Php Problem Overview


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" />

Php Solutions


Solution 1 - Php

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:

  1. The user literally clicks the submit button with the mouse or pointing device
  2. 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.

Solution 2 - Php

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
}

Solution 3 - Php

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

<input type="submit" name="Submit_this" id="This" />

Solution 4 - Php

Are you asking in php or 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.

If it is in js, use getElementById for that

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSonny BoyView Question on Stackoverflow
Solution 1 - PhpgoatView Answer on Stackoverflow
Solution 2 - PhpMiffTheFoxView Answer on Stackoverflow
Solution 3 - PhpKavishka HirushanView Answer on Stackoverflow
Solution 4 - PhpKarthikView Answer on Stackoverflow