PHP AJAX Tutorial
AJAX (Asynchronous JavaScript and XML) allows data to be sent and received from a server asynchronously without refreshing the web page. It's widely used for live search, chat, auto-refresh, etc.
Key Concepts of AJAX
Uses JavaScript (or jQuery) to send requests to the server.
Server processes the request (in PHP or other backend language).
The response is returned and used to update part of the page.
Data is often exchanged in JSON or plain text.
Server processes the request (in PHP or other backend language).
The response is returned and used to update part of the page.
Data is often exchanged in JSON or plain text.
AJAX (Asynchronous JavaScript and XML) एक तकनीक है जिससे आप वेबपेज को रीलोड किए बिना सर्वर से डेटा भेज और प्राप्त कर सकते हैं। इसका उपयोग लाइव सर्च, चैट और ऑटो-रीफ्रेश जैसी चीज़ों में होता है।
AJAX Example (Send Username to PHP)
<input type="text" id="username">
<button onclick="loadData()">Send</button>
<div id="result"></div>
<script>
function loadData() {
var name = document.getElementById("username").value;
var xhr = new XMLHttpRequest();
xhr.open("POST", "ajax-response.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onload = function () {
if (xhr.status === 200) {
document.getElementById("result").innerHTML = xhr.responseText;
}
};
xhr.send("username=" + encodeURIComponent(name));
}
</script>
PHP File (ajax-response.php)
<?php
if (isset($_POST['username'])) {
$name = htmlspecialchars($_POST['username']);
echo "Hello, $name! Welcome to AJAX.";
}
?>
Hello, Aryan! Welcome to AJAX.