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

WBOY
Lepaskan: 2016-07-21 15:48:31
asal
1073 orang telah melayarinya

<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...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan