Prevent Form resubmission upon hitting back button

Prevent Form resubmission upon hitting back button

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

Stefan Bogdanescu

Founder & Senior Architect

Prevent Form Resubmission Upon Hitting Back Button

When a user clicks a link to view a specific record and then hits the browser's "Back" button, the browser attempts to restore the previous state. If that previous state involved a form submission, modern browsers trigger a confirmation dialog asking if you really want to reload the page. In your specific scenario, this happens because the database.php file checks for a POST variable (apply_filter) and executes logic based on it. When you navigate back, the browser re-sends the last POST request containing that filter value.

The root cause here isn't just bad UX; it is a direct consequence of how HTTP statelessness interacts with browser history. The server sees a valid POST request with data attached to it. Without intervention, the server processes this data exactly as if the user had just clicked the submit button. To stop this, you must break the association between the form submission and the current page state before the user navigates away.

The most robust solution is to clear the POST data immediately after processing it, but only when necessary. In your database.php file, you are currently checking if $_POST['apply_filter'] exists. If it does, you run your query. However, once that logic block finishes executing and the page renders, the $_POST array still contains the submitted data in memory (until the script ends). While clearing the array doesn't physically delete the request from the browser's history, it prevents the server from accidentally re-processing the form if a subsequent redirect or internal error occurs.

A more effective architectural change involves preventing the browser from saving the POST data to its history in the first place. You can achieve this using JavaScript. Immediately after the page loads (or specifically after the filter logic renders), execute a script that clears the form fields and, crucially, removes the form element from the DOM or prevents it from being submitted via back navigation.

Here is a practical implementation strategy using AJAX to handle the filtering, which inherently avoids this issue entirely. Instead of submitting the form with $_POST, you intercept the submit event:

// database.php (Refactored approach)
<form id="filterForm">
    <select name="filter">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <input type="submit" name="apply_filter" value="Filter">
</form>

<script>
document.getElementById('filterForm').addEventListener('submit', function(event) {
    event.preventDefault(); // Stop standard form submission
    
    var formData = new FormData(this);
    
    fetch('', {
        method: 'POST',
        body: formData
    })
    .then(response => response.text())
    .then(html => {
        document.body.innerHTML = html;
        // The new HTML does not contain the old form, so history won't trigger a resubmission
    });
});
</script>

By using fetch and swapping out the entire page content (or appending results to a container), you ensure that when the user hits "Back", the browser cannot find a valid form to submit. The URL remains unchanged, but the state of the page is rendered fresh based on GET parameters or empty state, rather than relying on hidden POST data.

If refactoring to AJAX is not an option due to legacy constraints, you must ensure that the link generation in database.php does not inadvertently include form fields in the output context that confuse the browser. However, since your links are simple view.php?id=X, this is unlikely to be the issue. The real fix lies in ensuring the server doesn't treat a "Back" button click as a new submission event.

In a Laravel application, this behavior is handled gracefully by default because forms usually submit via AJAX or redirects occur after processing (the "Redirect after Save" pattern). If you are migrating to a modern stack like Laravel, consider utilizing Route::get for your views and handling the filtering logic strictly within controllers that return JSON or Blade views. This decouples the view rendering from the form submission logic, effectively neutralizing the back-button resubmission risk without needing complex DOM manipulation.

Ultimately, the goal is to ensure that every interaction with your application can be fully described by its URL. If a page needs data to render, it should accept it via query parameters (GET) rather than relying on hidden POST variables that persist in history. By moving toward an architecture where the current state is always retrievable from the URL, you eliminate the need for browser confirmation dialogs entirely.

Our network