In this example, we are creating a XMLHttpRequest object to send and receive data from a text file and we use a callback function to display the fetched data.
Ajax callback function
In this example, we are creating a XMLHttpRequest object to send and receive data from a text file and we use a callback function to display the fetched data.
Example
callbackfunction.php
<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function callbackFunction()
{
loadXMLDoc("TextDocument.txt",function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
});
}
</script>
</head>
<body>
<div id="myDiv"><h2>AJAX will change this text on button click</h2></div>
<button type="button" onclick="callbackFunction()">Change Content</button>
</body>
</html>
TextDocument.txt
<h3>DEVMANUALS</h3> <p>In Devmanuals, you get all the contents needed to learn ajax</p> <p>AJAX is a technique for creating fast and dynamic web pages.</p>
Output :

When click on button :


[ 0 ] Comments