9. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
D? li?u Server tr? v? s? ???c x? l? ??
hi?n th? t?i ??y
V┴ d?
testAjax.htm
<html>
<body>
<script src=/slideshow/web-course-php-ajax/23031422/"selectcustomer.js"></script>
<form>
Select a Customer:
<select name="customers^ onchange="showCustomer(this.value)">
<option value="ALFKI">Alfreds Futterkiste</option>
<option value="NORTS ">North/South </option>
<option value="WOLZA">Wolski Zajazd</option>
</select>
</form>
<p>
<div id="txtHint"><b>Customer info will be listed here.</b></div>
</p>
</body>
</html>
10. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
AJAX C Browser Support
<script type="text/javascript">
xmlHttp=GetXmlHttpObject();
function GetXmlHttpObject(){
var objXMLHttp=null
if (window.XMLHttpRequest){
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject){
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
</script>
Tr━nh duy?t
Firefox/Netscape´
Tr━nh duy?t IE
Kh?i t?o XMLHttp
11. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
AJAX C ??i t??ng XMLHttpRequest
xmlHttp.onreadystatechange=stateChanged;
function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
Thi?t l?p h┐m x? l? d?
li?u tr? v? t? Server
D? li?u tr? v? t?
Server
12. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
AJAX C G?i Request l┷n Server
function showCustomer(str){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
return;
}
xmlHttp.onreadystatechange=stateChanged;
var url=^getcustomer.php^;
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
G?i request l┷n
server
13. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
AJAX - The Server-Side ASP Script
Getcustomer.php
<?php
$connection = mysql_connect("localhost","fred","shhh");
mysql_select_db("winestore", $connection);
if (isset($_GET["q"])){
$sql="SELECT * FROM CUSTOMER WHERE CUST_ID='" . $_GET["q"] . "'";
$result = mysql_query ($sql, $connection);
// Show table
while ($row = mysql_fetch_array($result, MYSQL_NUM){
´
}
}
?>