Use an open-source Ajax Library: Ajax toybox
Description:
When registering or checking out a shopping cart, you need to fill in your personal data. This step can be concise. We only need to fill in the zip code for the guests and then follow the zip code, the database automatically retrieves the corresponding province, city, and other address information.
This not only reduces customer input, increases customer experience, but also reduces data input errors.
Implementation:
Html
<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 );
?>
When the customer enters a postcode, zipcode. php receives it, extracts the corresponding data from the data table, and then press
Return 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];
}
Final:
Author's blog:Http://blog.csdn.net/jxyuhua/