One form with two submit buttons and different actions for each button

One form with two submit buttons and different actions for each button

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

Stefan Bogdanescu

Founder & Senior Architect

One Form, Two Submit Buttons with Distinct Actions

You have a single HTML form tag pointing to formindb_hoh_1.php. Inside that same form, you want two submit buttons: one that triggers an action in formindb_hoh_1.php and another that triggers an action in formindb_hoh_2.php. The immediate challenge is that the <form> element has only one action attribute. When the user clicks either button, the browser sends a POST request to that single URL. If you simply change the name or value of the inputs, the server receives the same request data but doesn't know which "button" caused the submission unless you explicitly handle that in your backend logic.

The standard solution involves separating the form submission into two distinct events. You cannot achieve this purely with HTML attributes on the buttons themselves while keeping a single <form> tag pointing to one URL. To get different endpoints for each button, you must either use JavaScript to intercept the submission or restructure the form so that only the specific data relevant to that action is sent.

Intercepting Submission with JavaScript

The most flexible approach is to prevent the default form submission behavior and manually dispatch an AJAX request to the intended endpoint based on which button was clicked.

In your HTML, keep a single form container but remove the action attribute or set it to a common handler. Then, attach event listeners to each submit button:

<form id="columnarForm" enctype="multipart/form-data">
    <!-- Your existing inputs here -->
    
    <input type="image" name="camper" value="camper" src="../images/apps/camperBtn.png" class="submit_button" id="btnCamper">
    <input type="image" name="medical" value="medical" src="../images/apps/medicalBtn.png" class="submit_button" id="btnMedical">
</form>

<script>
document.getElementById('columnarForm').addEventListener('submit', function(event) {
    event.preventDefault(); // Stop the browser's default form submission
    
    const clickedButton = event.submitter;
    const formData = new FormData(this);
    
    if (clickedButton === document.getElementById('btnCamper')) {
        // Send data to formindb_hoh_1.php
        fetch('formindb_hoh_1.php', {
            method: 'POST',
            body: formData,
            // Handle multipart/form-data correctly if necessary for file uploads
        })
        .then(response => response.json())
        .then(data => console.log('Camper action:', data));
    } else if (clickedButton === document.getElementById('btnMedical')) {
        // Send data to formindb_hoh_2.php
        fetch('formindb_hoh_2.php', {
            method: 'POST',
            body: formData,
        })
        .then(response => response.json())
        .then(data => console.log('Medical action:', data));
    }
});
</script>

This pattern ensures that the browser never navigates away from the current page unexpectedly. However, you must ensure your backend scripts (formindb_hoh_1.php and formindb_hoh_2.php) are prepared to handle the request context. Since both buttons share the same form ID, your PHP logic needs to inspect which button was pressed. You can do this by checking if a specific hidden field exists or by relying on the name attribute of the submitted input.

A common pitfall is forgetting to clear the form data after submission or not handling file uploads correctly with AJAX. If you are uploading files, ensure your fetch request includes the necessary headers and that your PHP scripts use $_FILES rather than $_POST for those specific fields.

The Hidden Field Approach

If you prefer a server-side-only solution without JavaScript dependencies, you can add hidden input fields to differentiate the actions. Each button click would programmatically populate a different hidden field before submission. While this avoids AJAX complexity, it requires manipulating the DOM via JavaScript to set the hidden value and then letting the form submit naturally. This hybrid approach is less clean than pure AJAX but might fit into legacy systems where fetch is unsupported.

Alternatively, you can use two separate <form> tags stacked on top of each other, sharing the same layout structure but with different IDs. In this scenario, one form contains only the "camper" button and the other contains only the "medical" button. You would toggle their visibility using CSS or JavaScript based on user interaction. This eliminates the need for custom event handling entirely, as each form naturally submits to its own unique action URL.

<!-- Form 1: Only handles camper action -->
<form id="formCamper" action="formindb_hoh_1.php" enctype="multipart/form-data">
    <!-- Shared inputs -->
    <input type="hidden" name="action_type" value="camper">
</form>

<!-- Form 2: Only handles medical action -->
<form id="formMedical" action="formindb_hoh_2.php" enctype="multipart/form-data" style="display:none;">
    <!-- Shared inputs -->
    <input type="hidden" name="action_type" value="medical">
</form>

In this configuration, you hide the second form and show it only when the medical button is clicked. This method is robust for older browsers but adds DOM complexity to manage visibility states.

Conclusion on Implementation Strategy

The choice between intercepting with JavaScript or splitting the forms depends on your project's constraints. If you are using a modern PHP stack or a framework like Laravel, intercepting the submission allows you to route the request cleanly without cluttering the view logic. However, if you are maintaining a legacy application where AJAX is risky, splitting the forms or utilizing hidden fields triggered by DOM events remains a viable fallback.

Remember that regardless of the method chosen, your backend scripts must validate the incoming data and determine the intended action before processing files or database writes. Never rely solely on the button's name attribute to distinguish actions without explicit logic in your PHP handlers.

Our network