際際滷

際際滷Share a Scribd company logo
L?p tr━nh v┐ Thi?t k? Web 1
? 2007 Khoa C?ng ngh? th?ng tin
Khoa CNTT C ?H.KHTN
AJAX
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
AJAX
Asynchronous JavaScript And Xml
AJAX
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
V┴ d?
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
Trang web
Submit
Server
Reload
New
page
Kh?ng d┫ng ajax
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
D┫ng ajax
Trang web
Server
Update
javascript
Response
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
function onClick()// Ajax function
{
var xmlHttp;
xmlHttp.open("GET",^serverURL^,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//x? l┴ d? li?u response;
}
}
}
Kh?i t?o ??i t??ng xmlHttp
´´
echo (^noidung ̄);
......
Client Server
Code?
L?p tr━nh v┐ Thi?t k? Web 1 C AJAX
? 2007 Khoa CNTT - ?HKHTN
State Description
0 Request ch?a ???c kh?i t?o
1 Request ?? ???c thi?t l?p
2 Request ?? ???c g?i
3 Request ?ang ???c x? l┴
4 Request ???c x? l┴ xong
C│c tr?ng th│i c?a thu?c t┴nh
readyState
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//X? l┴ d? li?u nh?n ???c
}
}
Code?
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>
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
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
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
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){
´
}
}
?>

More Related Content

Web course php ajax

  • 1. L?p tr━nh v┐ Thi?t k? Web 1 ? 2007 Khoa C?ng ngh? th?ng tin Khoa CNTT C ?H.KHTN AJAX
  • 2. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN AJAX Asynchronous JavaScript And Xml AJAX
  • 3. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN V┴ d?
  • 4. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN
  • 5. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN Trang web Submit Server Reload New page Kh?ng d┫ng ajax
  • 6. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN D┫ng ajax Trang web Server Update javascript Response
  • 7. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN function onClick()// Ajax function { var xmlHttp; xmlHttp.open("GET",^serverURL^,true); xmlHttp.send(null); xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //x? l┴ d? li?u response; } } } Kh?i t?o ??i t??ng xmlHttp ´´ echo (^noidung ̄); ...... Client Server Code?
  • 8. L?p tr━nh v┐ Thi?t k? Web 1 C AJAX ? 2007 Khoa CNTT - ?HKHTN State Description 0 Request ch?a ???c kh?i t?o 1 Request ?? ???c thi?t l?p 2 Request ?? ???c g?i 3 Request ?ang ???c x? l┴ 4 Request ???c x? l┴ xong C│c tr?ng th│i c?a thu?c t┴nh readyState xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //X? l┴ d? li?u nh?n ???c } } Code?
  • 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){ ´ } } ?>