HTML form submit to PHP script

HTML form submit to PHP script

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

Stefan Bogdanescu

Founder & Senior Architect

HTML Form Submit to PHP Script

The most common stumbling block for beginners handling form submissions isn't understanding server-side logic; it's getting the client-side data structured correctly so the backend can read it. You've identified a classic conflict: you want a single $_POST variable named website_string to capture a value from a <select> dropdown, but your submit button is currently overwriting that value with its own value attribute.

In HTML forms, every input field shares the same name space. When the form submits via method="post", the browser serializes all inputs into a query string (or POST body). If two inputs share the exact same name attribute, the last one sent wins. In your current setup:

<select name="website_string">
    <option value="" selected></option>
    <option VALUE="abc"> ABC</option>
    <!-- ... -->
</select>
<input type="submit" name="website_string" value="selected">

When the user clicks "Submit", the browser sends website_string twice. Once with the value chosen from the dropdown (e.g., abc) and once with the button's label or explicit value (selected). PHP's $_POST['website_string'] will simply contain the string "selected" because the input element appears last in the serialization order.

To fix this, you must decouple the selection mechanism from the submission trigger. The standard pattern is to give the submit button a unique name, often submit, or no name at all if you don't need it for processing. This ensures the dropdown value remains intact.

Here is the corrected HTML structure:

<form action="chk_kw.php" method="post">
    <select name="website_string">
        <option value="" selected>Choose an option</option>
        <option value="abc">ABC</option>
        <option value="def">def</option>
        <option value="hij">hij</option>
    </select>
    
    <!-- The name here doesn't conflict with the select field -->
    <input type="submit" name="submit_btn" value="Submit">
</form>

Now, when you access $_POST['website_string'] in your chk_kw.php script, it will correctly return abc, def, or hij. The button's action is purely to trigger the POST request; it should not participate in the data payload unless specifically required for a multi-step wizard.

Handling the Empty Option

Your source code includes an empty option: <option value="" selected>. This is a valid pattern for allowing users to skip a selection, but you must handle the empty string explicitly in your PHP logic. If website_string is equal to "...", it means no valid choice was made. You might want to redirect the user or display an error message rather than processing the request:

if (isset($_POST['submit_btn'])) {
    $site = $_POST['website_string'];

    if (empty($site)) {
        // Handle empty selection
        die('Please select a website string.');
    }

    // Process valid data
    // echo "Selected: " . htmlspecialchars($site);
}

Trade-offs and Best Practices

One trade-off to consider is the naming convention for the submit button. While leaving it nameless works, explicitly naming it (like submit_btn) allows your PHP script to distinguish between a form submission triggered by that specific button versus other potential actions on the same page. This is particularly useful if you later expand the form with multiple submit buttons or AJAX handlers.

Another pitfall involves case sensitivity in HTML attributes. While modern browsers are largely forgiving, it is best practice to be consistent. Your example mixed name (lowercase) and VALUE (uppercase). Always use lowercase for attribute names like name, value, action, and method to ensure compatibility across different environments and frameworks.

If you are building larger applications where form state management becomes complex, consider using a framework approach. Laravel provides robust form helpers that abstract away these naming collisions automatically. For instance, using the @csrf directive and named forms ensures that every field is uniquely identified, preventing accidental overwrites of POST data. You can explore further resources on secure form handling at https://laravelcompany.com.

Ultimately, the rule is simple: never give your submit button the same name as a data-carrying input field unless you specifically intend for the button's value to be part of the payload. By separating concerns—selection versus submission—you ensure your backend receives exactly the data the user intended to send.

Our network