1 PHÁT TRIỂN ỨNG DỤNG WEB Bài 4 PHP Ajax Nguyễn Hữu Thể 2 AJAX ➢AJAX is about updating parts of a web page, without reloading the whole page − AJAX = Asynchronous JavaScript and XML 3 AJAX − Web page[.]
PHÁT TRIỂN ỨNG DỤNG WEB Bài 4: PHP Ajax Nguyễn Hữu Thể AJAX ➢ AJAX is about updating parts of a web page, without reloading the whole page − AJAX = Asynchronous JavaScript and XML AJAX − Web page can communicate with a web server while a user type characters in an input field AJAX - Example function showHint(str) { if (str.length == 0) { document.getElementById("txtHint").innerHTML = ""; return; } else { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == && this.status == 200) { document.getElementById("txtHint").innerHTML = this.responseText; } }; xmlhttp.open("GET", "gethint.php?q=" + str, true); xmlhttp.send(); } }Start typing a name in the input field below:
First name:Suggestions:
AJAX - Example gethint.php // get the q parameter from URL $q = $_REQUEST["q"]; $hint = ""; AJAX Database − AJAX can be used for interactive communication with a database ✓ The database table we use in the example above looks like this: id FirstName LastName Age Hometown Job Peter Griffin 41 Quahog Brewery Lois Griffin 40 Newport Piano Teacher Joseph Swanson 39 Quahog Police Officer Glenn Quagmire 41 Quahog Pilot ✓ Fetch information from a database with AJAX ajax_database.php AJAX Database function showUser(str) { if (str == "") { document.getElementById("txtHint").innerHTML = ""; return; } else { 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 = function() { if (this.readyState == && this.status == 200) { document.getElementById("txtHint").innerHTML = this.responseText; } }; xmlhttp.open("GET","getuser.php?q="+str,true); xmlhttp.send(); } } ... AJAX is about updating parts of a web page, without reloading the whole page − AJAX = Asynchronous JavaScript and XML AJAX − Web page can communicate with a web server while a user type characters... LastName Age Hometown Job Peter Griffin 41 Quahog Brewery Lois Griffin 40 Newport Piano Teacher Joseph Swanson 39 Quahog Police Officer Glenn Quagmire 41 Quahog Pilot ✓ Fetch information from