Introduction to AJAX
What is AJAX?
-
AJAX = Asynchronous JavaScript and XML
-
Technique to send and receive data from a server without reloading the webpage.
-
Creates faster, more dynamic, user-friendly websites.
-
Nowadays, JSON is used more than XML for data exchange.
How Does AJAX Work?
-
JavaScript sends an HTTP request to the server (usually via
XMLHttpRequestorfetch). -
Server processes the request (e.g., PHP script).
-
Server returns data (JSON, HTML, text).
-
JavaScript receives the response and updates the webpage dynamically.
Basic AJAX Example Using XMLHttpRequest
PHP Script (data.php)
Modern AJAX with fetch()
Sending Data via POST with AJAX & PHP
JavaScript (fetch POST example)
PHP (process.php)
Summary
| Step | Description |
|---|---|
| Create AJAX Request | Use XMLHttpRequest or fetch() in JS |
| Server-side Script | PHP (or other) handles request and returns data |
| Update Page | JS updates the DOM dynamically without reload |
