PHP + AJAX: PHP Tutorial on zip code processing

Source: Internet
Author: User
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 | )....

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.