AJAX据邮政编码自动完成城市和地址信息

来源:互联网 发布:java 验证码 编辑:程序博客网 时间:2024/04/27 17:17
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
【导读】本文用AJAX实现在注册或购物车结帐的时候,只需客人填入邮政编码,然后就根据这个邮政编码,自动从数据库中取出相应的省,市等地址信息

描述:在注册或购物车结帐的时候,需要用户填入个人资料,这个环节可以简练一下,我们只需客人填入邮政编码,然后就根据这个邮政编码,自动从数据库中取出相应的省,市等地址信息

这样既可以减少客户的输入,增加客户体验,又可以减少由于资料输入而产生的错误。

实现:

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

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

以上是客户输入页面,下面是服务端的处理页面'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);

?>

当客户输入一个POSTCODE后,zipcode.php就接收到它,然后进行从数据表中取出对应的资料,再按一定的格式返回给客户端(此处是以 | 分隔)。最后客户端接收返回的资料,显示在页面上。

if(response.indexOf('|' != -1)) {

update = response.split('|');

document.getElementById("city").value = update[0];

document.getElementById("state").value = update[1];

}

最终的效果图:

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>