¡¡¡¡²ÉÓÃÒ»¸ö¿ªÔ´µÄAJAX¿â£ºAjax Toybox
¡¡¡¡ÃèÊö£º
¡¡¡¡ÔÚ×¢²á»ò¹ºÎï³µ½áÕʵÄʱºò£¬ÐèÒªÓû§ÌîÈë¸öÈË×ÊÁÏ£¬Õâ¸ö»·½Ú¿ÉÒÔ¼òÁ·Ò»Ï£¬ÎÒÃÇÖ»Ðè¿ÍÈËÌîÈëÓÊÕþ±àÂ룬Ȼºó¾Í¸ù¾ÝÕâ¸öÓÊÕþ±àÂ룬×Ô¶¯´ÓÊý¾Ý¿âÖÐÈ¡³öÏàÓ¦µÄÊ¡£¬ÊеȵØÖ·ÐÅÏ¢¡£ÕâÑù¼È¿ÉÒÔ¼õÉÙ¿Í»§µÄÊäÈ룬Ôö¼Ó¿Í»§ÌåÑ飬ÓÖ¿ÉÒÔ¼õÉÙÓÉÓÚ×ÊÁÏÊäÈë¶ø²úÉúµÄ´íÎó¡£
¡¡¡¡ÊµÏÖ£º
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];
}