Send value of submit button when form gets posted

Send value of submit button when form gets posted

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

Stefan Bogdanescu

Founder & Senior Architect

Send Value of Submit Button When Form Gets Posted

The core issue lies in how HTML forms handle multiple inputs of the same type, specifically when dealing with submit buttons. In your provided code snippet, you have defined two <input> elements with type="submit", but they share the identical attribute id='submit'. While modern browsers generally ignore duplicate IDs without crashing the page, this specific duplication is the root cause of why your PHP script cannot distinguish between the choices.

When a form uses method="post", the browser serializes the data into the request body. For standard <input> and <textarea> elements, the browser includes only the first element it encounters if they share the same name attribute. However, submit buttons behave differently; they are processed based on their name attribute to populate the key in the $_POST array.

In your example, you have assigned different name attributes (Tea and Coffee) to the two buttons, which is technically correct for distinguishing them. However, you also gave them the same id. While browsers are lenient with duplicate IDs, it is a violation of HTML standards and can cause issues with CSS selectors or JavaScript event listeners. More importantly, if you had accidentally given them the same name attribute in your original attempt (which often happens when cloning forms), the browser would send only one value, overwriting the other before the request even hits the server.

Correcting the HTML Structure

To ensure both values reach buy.php, you must guarantee that each submit button has a unique and distinct name attribute. The id attribute is not required for form submission logic; it should be unique if used for styling or scripting, but the name attribute is strictly what drives the POST data.

Here is the corrected HTML structure:

<form action="buy.php" method="post">
    <select name="name">
        <option value="John">John</option>
        <option value="Henry">Henry</option>
    </select>
    
    <!-- Ensure unique names for each submit button -->
    <input type="submit" name="tea_order" value="Tea">
    <input type="submit" name="coffee_order" value="Coffee">
</form>

In this revision, the name attributes are now distinct (tea_order and coffee_order). Consequently, your PHP script in buy.php should be updated to reflect these specific keys:

<?php
    $name = $_POST['name'];
    
    // Check which button was clicked based on the unique name
    if (isset($_POST['tea_order'])) {
        $purchase = 'Tea';
    } elseif (isset($_POST['coffee_order'])) {
        $purchase = 'Coffee';
    } else {
        // Handle case where no submit button was clicked
        $purchase = null; 
    }

    // Here some SQL database magic happens
    // Example: INSERT INTO orders (product, customer_name) VALUES ($purchase, $name);
?>

Handling the Logic in PHP

It is crucial to use isset() or check for key existence before accessing $_POST values. If a user clicks outside the form or if JavaScript intercepts the click event without submitting, the keys might be missing. Directly accessing $_POST['tea_order'] when that button wasn't clicked will result in an undefined index notice in older PHP versions or null in newer ones with error reporting settings.

Furthermore, consider using a hidden input field to explicitly store the intent if your form logic is complex. For instance, you could use name="action" instead of unique names for every submit button. This creates a cleaner separation between the action type and the data payload.

Modern Alternatives with Laravel

If you are building applications using Laravel, handling this scenario becomes even more straightforward due to its robust form validation and request handling features. You can utilize the request() facade or controller methods to easily determine which button was pressed without cluttering your HTML with multiple distinct keys.

For example, in a Blade template:

<form method="POST" action="/buy">
    @csrf
    
    <select name="name">
        <option value="John">John</option>
        <option value="Henry">Henry</option>
    </select>

    <button type="submit" name="product" value="Tea">Tea</button>
    <button type="submit" name="product" value="Coffee">Coffee</button>
</form>

In the controller, you can validate the request:

public function buy(Request $request)
{
    $request->validate([
        'name' => 'required|in:John,Henry',
        'product' => 'nullable|string',
    ]);

    $product = $request->input('product'); // Will be null if no button clicked
    
    // Proceed with logic
}

This approach leverages Laravel's built-in capabilities to handle the $_POST superglobal safely, ensuring that your application remains secure against mass assignment vulnerabilities while keeping your code clean.

Our network