Home > Backend Development > PHP Tutorial > php AJAX实例根据邮编自动完成地址信息_PHP

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

WBOY
Release: 2016-06-01 12:24:17
Original
934 people have browsed it

Ajax

<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]; 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template