<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>
<H3> Enter A United States Zipcode, Then Tab
<Table align = "center">
<Tr>
<Td> Enter Zipcode: </td>
<Td> <input type = "text" id = "zipcode" name = "zipcode" onBlur = "sndReq (this. value);"/> </td>
</Tr>
<Tr>
<Td> City: </td>
<Td> <input type = "text" id = "city" name = "city"/> </td>
</Tr>
<Tr>
<Td> State: </td>
<Td> <input type = "text" id = "state" name = "state"/> </td>
</Tr>
</Table>
The above is the customer input page, and the following is the server processing page 'zipcode. PHP
<? 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];