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