Complete Guide to Form Submission Without Page Reload Using AJAX Technology

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: AJAX | Form Submission | XMLHttpRequest | No Refresh | JavaScript

Abstract: This article provides an in-depth exploration of how AJAX technology solves the page refresh issue caused by traditional form submissions. It details the usage of the XMLHttpRequest object, including request configuration, parameter passing, and response handling, while comparing the advantages and disadvantages of native JavaScript and jQuery implementations. Through comprehensive code examples and step-by-step analysis, it helps developers understand the core principles and best practices of asynchronous form submission.

Problem Background and Challenges

In traditional web development, form submissions typically cause complete page refreshes, which not only affect user experience but also reset page states. As mentioned by the user, when attempting to execute JavaScript functions to update page content upon form submission, the function results are reset due to page refresh, causing a "flashing" phenomenon.

In the original code, returning false in the onsubmit event prevents the default submission behavior, but it also prevents normal form data submission. This contradiction is precisely the core problem that AJAX technology aims to solve.

AJAX Technology Principles

AJAX (Asynchronous JavaScript and XML) allows web applications to communicate asynchronously with servers without reloading the entire page. This technology is implemented through the XMLHttpRequest object, which can send HTTP requests in the background and handle responses, enabling partial page updates.

Key advantages include:

Native JavaScript Implementation

Here is a complete implementation based on XMLHttpRequest:

function submitForm() {
    // Create XMLHttpRequest object
    var http = new XMLHttpRequest();
    
    // Get form data
    var searchValue = document.getElementById('search').value;
    
    // Configure request parameters
    http.open("POST", "process.php", true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    // Prepare data to send
    var params = "search=" + encodeURIComponent(searchValue);
    
    // Send request
    http.send(params);
    
    // Handle response
    http.onload = function() {
        if (http.status === 200) {
            // Success response handling
            updateTable();
            console.log("Form submission successful: " + http.responseText);
        } else {
            // Error handling
            console.error("Request failed with status: " + http.status);
        }
    };
    
    // Error handling
    http.onerror = function() {
        console.error("Network error");
    };
}

// Modify form event handling
<form action="" method="get" onsubmit="submitForm(); return false;">
    <input id="search" name="search" type="text">
    <input type="submit" value="Search">
</form>

Code Analysis and Optimization

Key improvements in the above code include:

jQuery Implementation Solution

As a supplementary solution, jQuery provides a more concise AJAX implementation:

$("#submitButton").click(function(event) {
    event.preventDefault();
    
    $.ajax({
        type: "POST",
        url: "process.php",
        data: $("#searchForm").serialize(),
        success: function(response) {
            updateTable();
            console.log("Submission successful: " + response);
        },
        error: function(xhr, status, error) {
            console.error("Submission failed: " + error);
        }
    });
});

Performance and Compatibility Considerations

When choosing an implementation solution, consider:

Best Practice Recommendations

In actual development, it is recommended to:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.