PHP + AJAX automatically completes the example: processing the zip code. After the customer enters a POSTCODE, zipcode. PHP receives it, extracts the corresponding data from the data table, and then returns it to the client in a certain format (separated by | ). After the customer enters a POSTCODE, zipcode. PHP receives it, extracts the corresponding data from the data table, and then returns it to the client in a certain format (separated by | ). Finally, the client receives the returned information and displays it on the page.
Script
Function createRequestObject (){
Var ro;
Var browser = navigator. appName;
If (browser = "Microsoft Internet Explorer "){
Ro = new ActiveXObject ("Microsoft. XMLHTTP ");
} Else {
Ro = new XMLHttpRequest ();
}
Return ro;
}
Var http = createRequestObject ();
Function sndReq (zip ){
Http. open ('GET', 'zipcode. PHP? Zip = '+ zip );
Http. onreadystatechange = handleResponse;
Http. send (null );
}
Function handleResponse (){
If (http. readyState = 4 ){
Var response = http. responseText;
Var update = new Array ();
If (response. indexOf ('| '! =-1 )){
Update = response. split ('| ');
Document. getElementById ("city"). value = update [0];
Document. getElementById ("state"). value = update [1];
}
}
}
Script
Enter A United States Zipcode, Then Tab
Enter Zipcode: |
|
City: |
|
State: |
|
The above is the customer input page, and the following is the server processing page 'zipcode. PHP
$ Dbuser = 'root ';
$ Dbpass = '20140901 ';
$ Cn = mysql_connect ("localhost", $ dbuser, $ dbpass );
$ Db = mysql_select_db ("ajax ");
$ SQL = "select city, state from zipcodes where zipcode =". $ _ REQUEST ['Zip'];
$ Rs = mysql_query ($ SQL );
$ Row = mysql_fetch_array ($ rs );
Echo $ row ['city']. "|". $ row ['state'];
Mysql_close ($ cn );
?>
After the customer enters a POSTCODE, zipcode. PHP receives it, extracts the corresponding data from the data table, and then returns it to the client in a certain format (separated by | ). Finally, the client receives the returned information and displays it on the page.
If (response. indexOf ('| '! =-1 )){
Update = response. split ('| ');
Document. getElementById ("city"). value = update [0];
Document. getElementById ("state"). value = update [1];
Separator (separated by | )....