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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When you place multiple submit buttons on a single form, the default behavior of the HTML specification creates a specific ambiguity: the request payload will only contain the name and value attributes of the button that triggered the submission. If both buttons share the same name attribute, or if they lack a name entirely, your PHP script receives no way to distinguish between a "Save" action and a "Delete" action based solely on the HTTP request parameters.

In the markup provided in your scenario, you have defined two distinct inputs with unique identifiers but identical types:

<input type="submit" id="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" value="Delete" />

As written, neither button has a name attribute. When the user clicks either button and submits the form, your PHP backend will likely see an empty $_POST array regarding these buttons. The browser sends the name/value pair only for the specific control that initiated the event. Without a shared name, there is no common key to check.

To resolve this, you must assign a unique name attribute to each button. This allows your server-side code to inspect the $_POST superglobal array and determine which action was requested. The standard pattern involves giving each submit button the same name but ensuring they have distinct values, or assigning them completely different names if the logic requires it.

Here is how you should modify the HTML:

<input type="submit" name="action" value="save_changes" id="btnSubmit" />
<input type="submit" name="action" value="delete" id="btnDelete" />

Alternatively, if your routing logic prefers explicit keys rather than a generic "action" flag:

<input type="submit" name="submit_save" value="Save Changes" id="btnSubmit" />
<input type="submit" name="submit_delete" value="Delete" id="btnDelete" />

On the server side, you then check $_POST['action'] or $_POST['submit_save'] respectively. This approach is robust because it relies on the fundamental behavior of form submission: the browser constructs the request body by iterating over form controls and including only those that were interacted with in the context of a submit event.

A common pitfall occurs when developers attempt to use hidden inputs to simulate this state, such as adding <input type="hidden" name="clicked_button" value="save_changes">. While functional, this adds unnecessary complexity if you can simply rely on the submit button's own attributes. However, there is a specific scenario where hidden inputs are necessary: when one of your buttons is a "Cancel" or "Reset" that does not trigger a form submission in the traditional sense, or when you need to handle multiple actions within a single request without reloading the page via AJAX. If you are using traditional full-page submissions, sticking to the name attribute on the submit inputs is the cleanest solution.

If you are working within a Laravel application, this logic translates seamlessly into your controller. You might define a route that handles both actions under a single path, inspecting the request parameter to route internal logic accordingly:

public function handle(Request $request)
{
    if ($request->input('action') === 'delete') {
        // Execute deletion logic here
    } elseif ($request->input('action') === 'save_changes') {
        // Execute save logic here
    }
}

This pattern keeps your routing clean and avoids the need for distinct endpoints for every minor variation of an action, provided the payload is unambiguous. It is worth noting that while id attributes are excellent for client-side JavaScript manipulation or accessibility tools like screen readers, they do not carry any weight in HTTP requests. Always rely on name attributes for data transmission logic.

Our network