AJAX Form Submission Without Page Reload
AJAX (Asynchronous JavaScript and XML) is a technique that allows web pages to load data asynchronously, meaning it can update parts of the page without reloading the entire page. AJAX Form submission without page reload allows you to submit form data to the server and receive a response without refreshing the page.
Example: AJAX Form Submission Without Page Reload
Here's an example using JavaScript (jQuery) to submit a form using AJAX without reloading the page.
1. HTML Form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX Form Submission Without Page Reload</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Submit Form Using AJAX</h1>
<form id="ajax-form">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<button type="submit">Submit</button>
</form>
<div id="response"></div>
<script>
$(document).ready(function() {
$("#ajax-form").submit(function(e) {
e.preventDefault(); // Prevent the default form submission
var formData = $(this).serialize(); // Serialize form data
$.ajax({
url: 'submit.php', // PHP script to handle the form submission
type: 'POST',
data: formData,
success: function(response) {
$("#response").html(response); // Display response without page reload
},
error: function() {
alert('Error submitting form');
}
});
});
});
</script>
</body>
</html>
2. PHP Backend (submit.php):
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
// Process the data or save it to the database
// For this example, we will just return a message.
echo "Hello, " . htmlspecialchars($name) . ". Your email is " . htmlspecialchars($email) . ".";
}
?>
Explanation:
-
HTML Form:
- A simple form with two fields:
name
andemail
. The form has theid="ajax-form"
.
- A simple form with two fields:
-
AJAX Setup:
- When the form is submitted, the default page reload is prevented by
e.preventDefault()
. - The form data is serialized into a query string using
$(this).serialize()
. - An AJAX request is sent to the PHP file (
submit.php
), which processes the form data.
- When the form is submitted, the default page reload is prevented by
-
AJAX Request:
- The
url
specifies where the form data should be submitted (tosubmit.php
). - The
type: 'POST'
sends the data using the POST method. - Upon success, the
response
from the server is displayed inside the#response
div.
- The
-
PHP Script (
submit.php
):- The form data (
name
andemail
) is received via$_POST
. - The response is sent back to the browser, which is displayed without reloading the page.
- The form data (
Benefits of AJAX Form Submission:
- No Page Reload: The page doesn't reload after form submission, providing a seamless user experience.
- Asynchronous: The form submission and page interaction happen without interrupting the user’s activity.
- Server-Side Processing: The server still processes the form, but the results are returned without a page refresh.
This technique is widely used in modern web applications for real-time updates and interactive forms.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.