The load () method loads the data from the server and puts the returned data into the selected element. is a local method and must be given a subject;
$ (selector). Load (url,data,callback);
The required URL parameter specifies the URL that you want to load.
The optional data parameter specifies the collection of query string key/value pairs that are sent with the request. Send by default is get, the parameter becomes post when it exists;
The optional callback parameter is the function name that is executed after the load () method completes. It consists of three parameters:
--responsetxt-Contains the result content when the call succeeds, Statustxt-contains the state of the call, XHR-contains the XMLHttpRequest object
The Get () and post () methods are used to request data from the server over an HTTP get or POST request. is a global approach
$.get (Url,callback);
The required URL parameter specifies the URL that you want to request.
The optional callback parameter is the name of the function that executes after the request succeeds.
Callback it has two parameters: The data callback parameter holds the content of the requested page, and the status callback parameter holds the requested State
$.post (Url,data,callback);
The required URL parameter specifies the URL that you want to request.
The optional data parameter specifies the information to be sent along with the request.
The optional callback parameter is the name of the function that executes after the request succeeds. two parameters ibid.
Instance:
Background database data:
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 |
Html:
11 22 33 <script src= ' jquery.min ' type= ' Text/javascript ' ></script>44 <script>5566 </script>77 88 <body>99TenTen <form> One<select name= "Users" onchange= "Showuser (this.value)" > A<option value= "" >select a person:</option> -<option value= "1" >Peter</option> -<option value= "2" >Lois</option> the<option value= "3" >Glenn</option> -<option value= "4" >Joseph</option> -</select> -</form> +<br> -<div id= "Txthint" ><b>person info'll be listed here.</b></div> +21st A</body> atView CodePhp:
1<?PHP2$q =$_post["Q"];//receive with $_get (' Q ') when Send as GET3 4 5 6$con = mysql_connect (' localhost ', ' root ', ' Yangfan ');7 if(!$con) {8Die (' Could Not connect: '. Mysql_error ());9 }Ten One if(!mysql_select_db ("Ajax test")){ A Echo mysql_error (); - }; - the -$sql = "SELECT * from idcard WHERE id = $q"; -$result =mysql_query ($sql); - //$row = mysql_fetch_array ($result); + //Print_r ($row); -echo "<table border= ' 1 ' > + <tr> A <th>Firstname</th> at <th>Lastname</th> - <th>Age</th> - <th>Hometown</th> - <th>Job</th> -</tr> "; - in while($row =mysql_fetch_array ($result)) { - toecho "<tr>"; +echo "<td>". $row [' FirstName ']. "</td>"; -echo "<td>". $row [' LastName ']. "</td>"; theecho "<td>". $row [' Age ']. "</td>"; *echo "<td>". $row [' Hometown ']. "</td>"; $echo "<td>". $row [' Job ']. "</td>";Panax Notoginsengecho "</tr>"; - } theecho "</table>"; + A?>
View CodeLoad () method: Get Mode
1 functionShowuser (str) {2$ (' #txtHint '). Load (' demo.phpq= ' +str,function(RESPONSETXT,STATUSTXT,XHR) {4 if(statustxt== "Error"){5Alert ("Error:" +xhr.status+ ":" +xhr.statustext)6 }7 if(statustxt== "Success"){8Alert (' success! ')9 }Ten One }) A}
Load () Method: Post mode:
function Showuser (str) { $ (' #txtHint '). Load (' demo.php ', {q:str},function() { if (statustxt== "error") { alert ("error:" +xhr.status+ ":" +Xhr.statustext) } ) }
Get () Method:
1 functionShowuser (str) {2$.get (' demo.php?q= ' +str,function(data, status) {3 if(status== ' ERROR '){4Alert (' Failure ')5}Else{6$ (' #txtHint '). HTML (data);7 }8 9 });Ten One}
Post () Method:
1 function Showuser (str) {2 $.post (' demo.php ', {q:str},function(data, status) {3 $ (' #txtHint '). HTML (data); 4 }); 5 6 }
JQuery implements Ajax