Note:
Ajax Technology
Do not refresh the page, do a local refresh
Do not use form forms because they do not need to be submitted and are controlled by jquery
Must have an ID
If you want to use Ajax can use jquery can also be written in JS,
Recommended jquery because simple, direct introduction of jquery Package
JS and jquery do not operate the database, if you need to process the database also need to use the PHP language
jquery cannot embed PHP code directly, so you need to create a new page to write PHP code
jquery inside will need to pass the data past, submitted to the PHP page processing
1. Enter the username, password and click Login. Enter the correct jump page, enter the error prompt dialog box
Code for the login page
<! DOCTYPE html>$ (document). Ready (function(e) {$ ("#btn"). Click (function(){ varUID = $ ("#uid").Val (); varPWD = $ ("#pwd").Val (); $.Ajax ({//four parameters that must be written, in order of unlimitedURL: "cli.php",//process the path of the pageDATA:{U:UID,P:PWD},//the data passed. The number of commits is typically written in JSON format, and key is custom: The value that follows is the value aboveType: "POST",//Data delivery method, Get,post best Use POSTDataType: "TEXT",//type of return value, text,json,xml three types selectableSuccessfunction(data) {//If Ajax executes successfully, return to call the success function, which is the callback function, and the return value is returned as a parameter. if(data== "OK") {window. location= "Main.php"; } Else{alert (data); } }, }); }) });</script>View CodeCode to process the page
<?PHP//do the AJAX processing page, to shelf write <?php, do not empty line, because if the return is text, empty line, the returned result is not correct with a spaceinclude("dbda.php");$db=NewDbda ();$uid=$_post["U"];//Receive user name$pwd=$_post["P"];//Password$sql= "SELECT count (*) from login where Username= ' {$uid} ' and password= ' {$pwd}‘";$attr=$db->query ($sql);if($attr[0] [0]==0){ Echo"User name password is incorrect!";}Else{ Echo"OK";}
View Code2. Enter the user name to determine if there is any
Main Page code
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >$("#user"). Blur (function(){ varUID = $ (this).Val (); $.Ajax ({URL: "Chuli.php",Data: {U:uid},DataType: "TEXT",Success:function(d) {if(d== "OK") { $("#ts"). HTML ("User name available"); } Else { $("#ts"). HTML ("<span style= ' color:red ' > The user name already exists!</span>") } } }) });</script>View CodeWorking with pages
<?PHPinclude("dbda.php");$uid=$_post["U"];$sql= "SELECT count (*) from login where Username= ' {$uid}‘";$attr=$db->query ($sql);if($attr[0] [0] = = 0){ Echo"OK";}Else{ Echo"NO";}
View Code3. Click the Appear button, the drop-down menu appears, processing when the combination of the above two write to the same processing page
Query page
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >$ (document). Ready (function(e) {$ ("#btn"). Click (function(){ $.Ajax ({URL: "Chuli.php",Data: {Type:2},type: "POST",Datetype: "TEXT",Success:function(data) {varHang = data.Split("|"); varstr = ""; for(vari=0;i) { varLie = Hang[i].Split("^"); STR= str+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; } $("#sel").html (str); } }) })});</script>View CodeComprehensive Processing page
<?PHP$type=$_post["Type"];include(".. /.. /dbdaajax.php ");//include (".. /.. /dbda.php ");$db=NewDbdaajax ();Switch($type){ Case0:$uid=$_post["U"]; $pwd=$_post["P"]; $sql= "SELECT count (*) from login where Username= ' {$uid} ' and password= ' {$pwd}‘"; $attr=$db->query ($sql); if($attr[0] [0] = = 0) { Echo"The user password is not correct!"; } Else { Echo"OK"; } Break; Case1:$uid=$_post["U"]; $sql= "SELECT count (*) from login where Username= ' {$uid}‘"; $attr=$db->query ($sql); if($attr[0] [0] = = 0) { Echo"OK"; } Else { Echo"NO"; } Break; Case2:$sql= "SELECT * FROM Nation"; $attr=$db->ajax ($sql); Echo $attr; //to spell an array into a string /*$str = ""; for ($i =0; $i <count ($attr), $i + +) {for ($j =0; $j <count ($attr [$i]); $j + +) { $str = $str. $attr [$i] [$j]; $str = $str. " ^"; } $str = substr ($str, 0,strlen ($STR)-1); $str = $str. "|"; } $str = substr ($str, 0,strlen ($STR)-1); echo $str. " <br> "; */ Break;}
View CodeAjax Method Implementation Login page