首頁  >  文章  >  後端開發  >  php AJAX实例根据邮编自动完成地址信息_PHP教程

php AJAX实例根据邮编自动完成地址信息_PHP教程

WBOY
WBOY原創
2016-07-21 15:48:311077瀏覽

<script> <BR>function createRequestObject() { <BR>var ro; <BR>var browser = navigator.appName; <BR>if(browser == "Microsoft Internet Explorer"){ <BR>ro = new ActiveXObject("Microsoft.XMLHTTP"); <BR>}else{ <BR>ro = new XMLHttpRequest(); <BR>} <BR>return ro; <BR>} <BR>var http = createRequestObject(); <BR>function sndReq(zip) { <BR>http.open('get', 'zipcode.PHP?zip='+zip); <BR>http.onreadystatechange = handleResponse; <BR>http.send(null); <BR>} <BR>function handleResponse() { <BR>if(http.readyState == 4){ <BR>var response = http.responseText; <BR>var update = new Array(); <BR>if(response.indexOf('|' != -1)) { <BR>update = response.split('|'); <BR>document.getElementById("city").value = update[0]; <BR>document.getElementById("state").value = update[1]; <BR>} <BR>} <BR>} <BR></script>

Enter A United States Zipcode, Then Tab















Enter Zipcode:
City:
State:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/319751.htmlTechArticlescript function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn