Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Bài giảng Phát triển ứng dụng Web: Bài 4 PHP Ajax cung cấp cho người học những kiến thức như: Ajax; gethint.php; Ajax Database. Mời các bạn cùng tham khảo để nắm chi tiết nội dung bài giảng! | PHÁT TRIỂN ỨNG DỤNG WEB Bài 4 PHP Ajax Nguyễn Hữu Thể 1 AJAX AJAX is about updating parts of a web page without reloading the whole page. AJAX Asynchronous JavaScript and XML. 2 AJAX Web page can communicate with a web server while a user type characters in an input field 3 AJAX - Example AJAX - Example function showHint str if str.length 0 document.getElementById quot txtHint quot .innerHTML quot quot return else var xmlhttp new XMLHttpRequest xmlhttp.onreadystatechange function if this.readyState 4 amp amp this.status 200 document.getElementById quot txtHint quot .innerHTML this.responseText xmlhttp.open quot GET quot quot gethint.php q quot str true xmlhttp.send Start typing a name in the input field below First name Suggestions 4 get the q parameter from URL q _REQUEST quot q quot gethint.php hint quot quot lookup all hints from array if q is 5 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 1 Peter Griffin 41 Quahog Brewery 2 Lois Griffin 40 Newport Piano Teacher 3 Joseph Swanson 39 Quahog Police Officer 4 Glenn Quagmire 41 Quahog Pilot Fetch information from a database with AJAX 6 ajax_database.php AJAX Database function showUser str if str quot quot document.getElementById quot txtHint quot .innerHTML quot quot return else if window.XMLHttpRequest code for IE7 Firefox Chrome Opera Safari xmlhttp new XMLHttpRequest else code for IE6 IE5 xmlhttp new ActiveXObject quot Microsoft.XMLHTTP quot xmlhttp.onreadystatechange function if this.readyState 4 amp amp this.status 200 document.getElementById quot txtHint quot .innerHTML this.responseText xmlhttp.open quot GET quot quot getuser.php q quot str true xmlhttp.send 7 AJAX Database Select a person Peter Griffin Lois Griffin Joseph Swanson Glenn Quagmire Person info will be listed here. 8 Note When a user selects a person in the dropdown list above a function called quot showUser quot