Colleagues asked me, we get data from the database, the Ajax way to display the front page. Don't say anything, write a case.
1, create a page:
<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head> <Scripttype= "Text/javascript"src= "Ajax.js"></Script> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>Ajax Demo</title></Head><Body> <inputtype= "button"ID= "Txt1"onclick= "Showhint ()"value= "Get 123"/> <P>Digital:<spanID= "Txthint"></span></P> </Body></HTML>
2. Write a JS, which is Ajax
1 varXMLHTTP;2 functionShowhint ()3 {4 //defining a new array5 varArray =NewArray ();6 //Create a XMLHttpRequest object7 if(window. XMLHttpRequest)8{//code for ie7+, Firefox, Chrome, Opera, Safari modern browser9xmlhttp=NewXMLHttpRequest ();Ten } One Else A{//code for IE6, IE5 user low version IE -xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); - } the - //set the request type, request address, and whether to enable asynchronous processing requests, most settings turn on True -Xmlhttp.open ("GET", "gethint.php",true); - //sending requests to the server + xmlhttp.send (); - //Handling onReadyStateChange Events We specify the tasks that are performed when the server responds to readiness to be processed +Xmlhttp.onreadystatechange=function() A { at if(Xmlhttp.readystate==4 && xmlhttp.status==200) - { -document.getElementById ("Txthint"). innerhtml=Xmlhttp.responsetext; - } - } -}
3, OK, write a PHP bar, as a server. The data is false.
1<?PHP2 //populate an array with a name3 $a[]= "1";4 $a[]= "2";5 $a[]= "3";6 7 $hint="";8 for($i= 0;$i<Count($a);$i++)9 { Ten $hint=$hint." ".$a[$i]; One } A $response=$hint; - //Output Response - Echo $response; the?>
Look at the interface:
Click the button:
No more than a few more, come out.
PHP and Ajax get 123 of cases