-
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, making them faster and more interactive. Using AJAX, you can build real-world applications that respond to user actions immediately, enhancing user experience.
For example, Vicky can create applications where Sanjana interacts with the interface, and the data updates dynamically.
AJAX applications are web applications that use asynchronous requests to fetch or send data from the server without refreshing the page. Examples include:
Live search: Search suggestions appear as the user types.
Chat applications: Messages appear instantly without page reload.
Content feeds: News feeds or posts load dynamically.
Form submissions: Users submit forms and receive instant confirmation.
Data dashboards: Charts and tables update in real time.
AJAX separates client-side and server-side operations, making the application more efficient and responsive.
A live search application shows results as the user types, without waiting for a page reload.
<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();
});
search.php
)<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = $_GET['q'];
$sql = "SELECT name, email FROM users WHERE name LIKE '%$query%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<p>Name: ".$row['name'].", Email: ".$row['email']."</p>";
}
} else {
echo "No results found";
}
$conn->close();
?>
Explanation:
The search input sends queries to PHP via AJAX.
PHP fetches matching users from the database.
Results are displayed dynamically in the <div>
as Sanjana types.
AJAX allows chat messages to be sent and received without page reload.
<div id="chatBox"></div>
<input type="text" id="message" placeholder="Type a message">
<button id="send">Send</button>
function loadMessages() {
let xhr = new XMLHttpRequest();
xhr.open("GET", "get_messages.php", true);
xhr.onload = function() {
if(xhr.status === 200) {
document.getElementById("chatBox").innerHTML = xhr.responseText;
}
};
xhr.send();
}
// Load messages every 3 seconds
setInterval(loadMessages, 3000);
document.getElementById("send").addEventListener("click", function() {
let msg = document.getElementById("message").value;
let xhr = new XMLHttpRequest();
xhr.open("POST", "send_message.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onload = function() {
if(xhr.status === 200) {
document.getElementById("message").value = "";
loadMessages(); // Refresh chat
}
};
xhr.send("message=" + encodeURIComponent(msg));
});
Explanation:
Messages are sent to the server using POST.
Chat history is fetched periodically using GET.
Vicky can send a message, and Sanjana sees it instantly.
AJAX can be used to load content dynamically when a user clicks a button.
<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:
When the button is clicked, AJAX fetches posts from the server.
Posts are displayed dynamically without page reload.
Faster user experience: Only relevant data is loaded.
Interactive interface: Users like Sanjana can interact without refreshing.
Reduced server load: Only necessary requests are sent.
Real-time updates: Data can refresh automatically, e.g., chat or dashboards.
AJAX applications are widely used in social media, e-commerce, and content platforms.
AJAX applications allow developers like Vicky to build responsive, interactive web applications where Sanjana can:
Search live results while typing.
Chat in real time.
Load content dynamically on demand.
By combining AJAX with server-side scripts and databases, web applications become faster, smoother, and user-friendly, making the overall experience enjoyable for users.
Create a live search input that fetches user data from search.php
as the user types and displays results in a <div>
.
Build a chat interface where messages are sent via AJAX POST to send_message.php
and fetched every 3 seconds from get_messages.php
.
Create a button that loads blog posts from posts.php
via AJAX GET and displays them dynamically in a <div>
.
Write an AJAX request to fetch JSON data from users.php
and display only users older than 25.
Create a form to submit feedback via AJAX POST to feedback.php
and display a success message without reloading the page.
Write code to fetch a list of products from products.php
and dynamically display them in a table with name and price.
Build a “Load More” button that fetches additional posts from the server using AJAX and appends them to the existing list.
Create a search field that filters users by name and updates results live using AJAX and PHP.
Write an AJAX POST request to vote on a poll and dynamically update the results bar chart without reloading.
Create a function that automatically refreshes notifications every 5 seconds using AJAX GET and updates a <div>
on the page.
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