How to prevent page from reloading after form submit - JQuery

How to prevent page from reloading after form submit - JQuery

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

Stefan Bogdanescu

Founder & Senior Architect

Preventing Page Reloads After Form Submission in jQuery

The most common frustration for developers building dynamic interfaces is the moment a user clicks "Submit" and the entire page flashes white before data appears. This happens because, by default, an HTML form tag includes a submit event that triggers a full HTTP request to the server defined by its action attribute (or the current URL if omitted). When the server responds, the browser parses the HTML and re-renders the DOM, effectively wiping out any JavaScript state or partial updates you intended to show.

In the provided scenario, the jQuery .post() method is correctly sending the data to process.php. However, the issue isn't with how the AJAX call is initiated; it's with how the form element itself behaves once that click event fires. The browser does not wait for the asynchronous callback to finish before executing its default action.

To stop the page from reloading, you must intercept the form's default submit behavior. This is done by attaching an event listener to the form itself (or the submit button) using preventDefault().

Here is how you modify the jQuery logic:

$(document).ready(function () {
    $('#main').on('submit', function(event) {
        // Stop the browser from submitting the form normally
        event.preventDefault();

        var name = $("#name").val();
        var email = $("#email").val();

        $.post("process.php", {
            name: name,
            email: email
        }).done(function(response) {
            console.log("Success");
            // Update the UI based on response here
            $('#success_message').text(response);
            // Optionally reset the form
            $(this).trigger('reset'); 
        }).fail(function() {
            $('#success_message').text('Error submitting data.');
        });
    });
});

Notice two critical changes in this implementation. First, we attach the handler to #main (the form ID) rather than just the button. This ensures that if a user uses an enter key to submit or clicks the button, the handler catches it. Second, inside the event callback, event.preventDefault() is called immediately. This halts the default lifecycle of the form submission, preventing the browser from navigating away or reloading the page while your jQuery code continues to execute in the background.

There is a significant trade-off regarding how you handle the response data. In the original PHP snippet, the script outputs echo "successfully"; upon completion. Because the AJAX call is asynchronous, if you do not explicitly clear the form or update the DOM immediately after receiving this string, your input fields will still contain the old values when the user looks at the page again.

Furthermore, relying solely on console.log("Success") in production code is insufficient. The PHP script returns a plain text string ("successfully" or an error message). If you intend to show a success message directly on the page without reloading, you must capture that response and inject it into your DOM element with ID success_message.

Another common pitfall involves security. The provided PHP code uses direct variable interpolation: $save1 = "INSERT ... VALUES ('$Name', '$Email')". This is vulnerable to SQL injection attacks. If a user inputs malicious strings like ' OR '1'='1, the query structure breaks or allows unauthorized data access. A robust implementation should use prepared statements with bound parameters, which both protect against injection and handle escaping automatically.

Finally, consider the lifecycle of the form after submission. If your goal is to keep the form filled for a "batch entry" scenario, you might choose not to reset it. However, for a standard mailing list signup, resetting the inputs immediately after a successful insert provides a better user experience. You can achieve this by triggering a reset event on the form element after the AJAX success callback fires.

If you are moving from jQuery to a modern stack like Laravel, the underlying principle remains the same. Whether you are using Blade components with @csrf tokens or handling requests via controllers, preventing the default submission behavior is the standard way to handle single-page application interactions without full page reloads.

Our network