Ajax Example: Automatically complete address information based on ZIP code

Source: Internet
Author: User
Tags sql split zip client
Ajax Description:

In the registration or shopping cart checkout, users need to fill in personal data, this link can be concise, we need only guests fill in the ZIP code, and then according to this zip code, automatically from the database to remove the corresponding provincial, city and other address information. This can reduce the input of customers, increase the customer experience, but also reduce the error caused by data input.

  Realize:

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>
<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>

Above is the customer input page, below is the Service side processing page ' zipcode.php

<?php
$dbuser = ' root ';
$dbpass = ' 111111 ';

$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, the zipcode.php receives it, takes the corresponding data out of the datasheet, and returns it to the client in a certain format (here is a | Delimited). The last client receives the returned data, which is displayed on the page.

if (Response.indexof (' | '!=-1)) {
Update = Response.split (' | ');
document.getElementById ("City"). Value = Update[0];
document.getElementById ("state"). Value = Update[1];
}

The final effect chart:



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.