Ajax tutorial with examples in PHP
Retrieve data from a PHP file
Following Ajax program loads the content of the PHP (Hyper-text Pre-processor) file, named test1.php by clicking the button without reloading the whole program and the data of the test1.php will be set to the id “kk”( div id=”kk” line number 36). You can also see the data in alert box also.
PHP ajax example
<!--index.html--> <!DOCTYPE html> <html> <head> <script> function Url() { var u; if(window.XMLHttpRequest) { u=new XMLHttpRequest(); } else { u=new ActiveXObject("Microsoft.XMLHTTP") } u.onreadystatechange=function () { if(u.readyState==4 && u.status==200) { document.getElementById("kk"). innerHTML=u.responseText; } } u.open("GET","test1.php",true); u.send(); } </script> </head> <body> <h1>Load The PHP file data using Ajax</h1> <button type="button" onclick="Url()">Load File </button> <hr> <div id="kk"></div> </body> </html>
PHP FILE
<?php //test1.php echo "Welcome to the Ajax with PHP example
"; echo "Welcome to the Ajax with PHP example
"; echo "Welcome to the Ajax with PHP example
"; echo "Welcome to the Ajax with PHP example
"; ?>