HTML Submit form on dropdown change

HTML Submit form on dropdown change

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

Stefan Bogdanescu

Founder & Senior Architect

Submitting a Form on Dropdown Change Without a Button

You have successfully implemented a standard form submission where a user selects an option and clicks a submit button. The server-side logic in test.php correctly retrieves the value via $_POST['dropdown']. However, your goal is to eliminate the explicit submit button entirely, triggering the POST request immediately upon changing the selection.

The core technical constraint here is how HTML forms handle their submission target. When you use <form action="test.php">, the browser expects that URL to be populated by either an explicit action attribute or a dynamic modification of that attribute before the form is submitted. Simply passing a string like "test.php" directly inside the JavaScript event handler (e.g., this.form.submit("test.php")) will not work because the first argument expected by the submit method is the FormData object, not a URL string.

To achieve automatic submission upon dropdown change, you must explicitly set the form's action attribute in JavaScript before triggering the submission. The cleanest approach involves attaching an event listener to the <select> element that modifies the form and then calls the submit method.

Here is a pure HTML implementation that avoids external dependencies:

<form id="dynamicForm" action="" method="post">
    <select name="dropdown" onchange="handleSelectionChange()">
        <option value="">Select an option</option>
        <option value="value1">Option 1</option>
        <option value="value2">Option 2</option>
    </select>

    <!-- Fallback for accessibility and users without JS support -->
    <input type="hidden" name="fallback_action" value="default_handler.php">
</form>

<script>
    function handleSelectionChange() {
        const form = document.getElementById('dynamicForm');
        const formData = new FormData(form);

        // This is the critical step: assign the target URL to the action attribute
        form.action = 'test.php';

        // Trigger the submission
        form.submit();
    }
</script>

In this example, the <form> element starts with an empty action attribute. When the user selects an option, the onchange handler executes. The script creates a FormData object to inspect the current state (though strictly not necessary for submission, it is standard practice in complex scenarios) and then sets form.action to 'test.php'. Finally, form.submit() is called. This forces the browser to send the POST request to test.php with the selected value included in the body.

A critical consideration for this approach is accessibility and user experience. While modern browsers handle the lack of a submit button gracefully for automatic submissions, you must ensure there is still a way for users without JavaScript enabled to interact with the form. The HTML5 <noscript> tag is the standard solution here, providing a visible submit button that falls back if scripting is disabled. Without this fallback, screen readers and keyboard-only users might be unable to trigger the submission at all.

There are also trade-offs regarding performance and UX. Automatic submission on every change can feel janky if the target page is heavy or slow to load. If your dropdown options represent different views or pages that are frequently accessed, consider whether a standard "Load Data" button with an AJAX call might provide a smoother experience than a full page reload. However, for simple redirects or lightweight data fetches, the method described above remains robust and requires no external libraries.

If you are working within a Laravel application, this pattern translates easily. You would typically define your routes in web.php corresponding to the target file (e.g., Route::post('test', [TestController::class, 'handle'])) and ensure that the PHP session is handled correctly before redirecting or rendering the response. For more complex state management within Laravel, frameworks often provide helpers like redirect()->route('...', $request->only(...)), but the underlying mechanism of setting the form action in JavaScript remains the same.

Our network