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: