Ajax automatically completes the city and address information based on the zip code

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.