-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
AJAX allows web pages to communicate with the server without reloading, which is useful for building interactive web applications. In this tutorial, we’ll look at multiple practical examples of AJAX in action.
For instance, Vicky can create different features where Sanjana interacts with the page, and data updates dynamically.
A simple AJAX example is fetching a greeting from the server.
greet.php
)<?php
header('Content-Type: text/plain');
echo "Hello, Sanjana! Welcome to AJAX examples.";
?>
<button id="greetBtn">Get Greeting</button>
<div id="greetOutput"></div>
<script>
document.getElementById("greetBtn").addEventListener("click", function() {
let xhr = new XMLHttpRequest();
xhr.open("GET", "greet.php", true);
xhr.onload = function() {
if(xhr.status === 200) {
document.getElementById("greetOutput").textContent = xhr.responseText;
} else {
document.getElementById("greetOutput").textContent = "Error fetching greeting";
}
};
xhr.send();
});
</script>
Explanation:
The PHP script returns a plain text greeting.
Clicking the button fetches the greeting dynamically and displays it in the <div>
.
AJAX allows forms to submit data without reloading the page.
<input type="text" id="name" placeholder="Enter your name">
<button id="sendBtn">Send</button>
<div id="formResponse"></div>
document.getElementById("sendBtn").addEventListener("click", function() {
let name = document.getElementById("name").value;
let xhr = new XMLHttpRequest();
xhr.open("POST", "process.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onload = function() {
if(xhr.status === 200) {
document.getElementById("formResponse").textContent = xhr.responseText;
} else {
document.getElementById("formResponse").textContent = "Error sending data";
}
};
xhr.send("username=" + encodeURIComponent(name));
});
process.php
)<?php
if(isset($_POST['username'])){
$name = htmlspecialchars($_POST['username']);
echo "Hello, $name! Your data has been received.";
} else {
echo "No data received.";
}
?>
Explanation:
AJAX sends the form data using POST.
PHP processes the input and returns a response.
The page does not reload, keeping the interaction smooth.
AJAX can fetch JSON data and display it dynamically.
user.php
)<?php
header('Content-Type: application/json');
$user = array(
"name" => "Sanjana",
"age" => 26,
"email" => "sanjana@example.com"
);
echo json_encode($user);
?>
let xhr = new XMLHttpRequest();
xhr.open("GET", "user.php", true);
xhr.onload = function() {
if(xhr.status === 200) {
let user = JSON.parse(xhr.responseText);
document.getElementById("output").innerHTML =
`Name: ${user.name}<br>Age: ${user.age}<br>Email: ${user.email}`;
} else {
document.getElementById("output").textContent = "Failed to load data";
}
};
xhr.send();
Explanation:
PHP returns JSON data.
JavaScript parses it and displays user information dynamically.
Live search is a popular AJAX use case.
<input type="text" id="search" placeholder="Search users">
<div id="results"></div>
document.getElementById("search").addEventListener("keyup", function() {
let query = this.value;
if(query.length === 0) {
document.getElementById("results").innerHTML = "";
return;
}
let xhr = new XMLHttpRequest();
xhr.open("GET", "search.php?q=" + encodeURIComponent(query), true);
xhr.onload = function() {
if(xhr.status === 200) {
document.getElementById("results").innerHTML = xhr.responseText;
} else {
document.getElementById("results").textContent = "Error fetching results";
}
};
xhr.send();
});
Explanation:
The input field sends queries to PHP as the user types.
PHP returns matching results, displayed dynamically in the <div>
.
AJAX can fetch and display content dynamically on demand.
<button id="loadPosts">Load Posts</button>
<div id="posts"></div>
document.getElementById("loadPosts").addEventListener("click", function() {
let xhr = new XMLHttpRequest();
xhr.open("GET", "posts.php", true);
xhr.onload = function() {
if(xhr.status === 200) {
document.getElementById("posts").innerHTML = xhr.responseText;
} else {
document.getElementById("posts").textContent = "Failed to load posts";
}
};
xhr.send();
});
Explanation:
Clicking the button fetches server content via AJAX.
Content is displayed in the <div>
without reloading the page.
Improves user experience with fast, interactive interfaces.
Reduces page reloads, making websites smoother.
Allows real-time updates like live search and chat.
Works with different types of responses: text, HTML, JSON.
Vicky can implement multiple features, and Sanjana will experience instant updates and smoother interactions without waiting for full page reloads.
AJAX examples show how to:
Fetch text, HTML, or JSON dynamically.
Submit forms without refreshing the page.
Implement live search for instant results.
Load content dynamically on user actions.
Using these examples, developers can build responsive, interactive web applications that make users like Sanjana enjoy real-time updates and seamless interactions.
Write an AJAX GET request to fetch a greeting from greet.php
and display it in a <div>
when a button is clicked.
Create a form to submit a name via AJAX POST to process.php
and display the response without reloading the page.
Fetch JSON data from user.php
and display Sanjana’s name, age, and email dynamically.
Build a live search input that sends queries to search.php
and displays matching results in a <div>
.
Create a button that loads posts from posts.php
via AJAX GET and displays them dynamically in a <div>
.
Write an AJAX request that fetches user data and populates an HTML table with name and email.
Create a function to send feedback via AJAX POST to feedback.php
and display a confirmation message.
Write an AJAX GET request that automatically refreshes notifications every 5 seconds and updates a <div>
.
Build a simple chat interface where messages are sent via AJAX POST to send_message.php
and fetched periodically from get_messages.php
.
Create an AJAX request to filter users by age and dynamically display only users older than 25 in a <div>
.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts