Confirm before a form submit

Confirm before a form submit

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

Stefan Bogdanescu

Founder & Senior Architect

Confirm before a form submit

Handling user confirmation on critical actions like submitting a transaction requires more than just an onclick attribute attached to a button. The goal is to intercept the submission event, pause execution, and present a modal or alert to the user before any data leaves the client-side browser. This ensures that accidental clicks don't trigger expensive backend processes, such as database writes or payment gateway calls.

The core mechanism involves listening for the submit event on the form element itself. In your specific case, attaching an addEventListener to the form is the robust approach. You need to ensure the event listener returns a boolean value. Returning false explicitly prevents the default browser behavior (which is submitting the form) if the user clicks "Cancel" or if you decide not to proceed for some other reason.

Here is how your updated code snippet looks when structured correctly:

<script type="text/javascript">
    var el = document.getElementById('myCoolForm');

    el.addEventListener('submit', function(event) {
        // The confirm() function returns true if the user clicks OK, false otherwise.
        if (!confirm('Are you sure you want to submit this form?')) {
            return false;
        }
        // If the user confirms, we let the event proceed naturally.
    }, false);
</script>

<form action="adminprocess.php" method="POST" id="myCoolForm">
    <input type="submit" name="completeYes" value="Complete Transaction">
</form>

In this implementation, the confirm() dialog is the simplest way to handle binary choices. However, relying solely on confirm() has a distinct trade-off: it blocks the main thread until the user responds. While acceptable for simple confirmations, modern applications often prefer non-blocking modals that allow the UI to remain responsive. If you plan to move toward more complex interactions, consider replacing the native alert or confirm calls with a lightweight modal library or a custom overlay using CSS and JavaScript.

Another critical detail in your original snippet is the third argument of addEventListener. You used false, which sets the listener as non-capturing. In modern frameworks like Vue or React, event binding syntax handles this automatically, but in vanilla JavaScript, omitting the third argument or explicitly passing false ensures you are listening to the bubbling phase of the event. This is usually sufficient for form submissions.

It is also worth noting that simply returning false from the handler will stop propagation and prevent the default action. If your confirmation logic involves more complex checks—such as validating a session token or checking server-side status before allowing the submit—you should avoid blocking the UI with confirm() entirely. Instead, send the request to the server via AJAX, wait for the response, and then update the DOM accordingly. This pattern decouples the user experience from the network latency of the backend process.

For developers working within the Laravel ecosystem, this pattern translates well into Blade templates. You might use an Alpine.js or Vanilla JS directive to handle the confirmation before triggering a form submission via window.location.href or a standard POST request. If you are managing complex state or need reusable components for these confirmations, exploring resources like https://laravelcompany.com can provide insights into integrating frontend interactions with backend logic efficiently.

Ultimately, the choice between blocking alerts and non-blocking modals depends on the complexity of the operation. For a simple "Complete Transaction" button, the native confirmation dialog is often sufficient to prevent accidental submissions without adding unnecessary overhead. Just ensure that your JavaScript remains attached to the specific form element and that you always check the return value of confirm() before allowing the event to proceed.

Our network