本款php ajax應用實例是一款利用ajax+php調用城市二級聯動菜單,這樣我們可以實時的根據數據庫內容更新了。
本款php教程 ajax應用實例是一款利用ajax+php調用城市二級聯動菜單,這樣我們可以實時的根據數據庫教程內容更新了。
<html>
<head>
<title>ajax+php無刷新二級聯動菜單的應用實例</title><script language="網頁特效">
var xmlhttp = null;function getxmlhttprequest()
{
var xmlhttp=null;
try
{
xmlhttp = new xmlhttprequest();
}
catch(e)
{
try
{
xmlhttp = new activexobject("msxml2.xmlhttp");
}
catch (e)
{
try
{
xmlhttp = new activexobject("microsoft.xmlhttp");
}
catch(e)
{
xmlhttp = false;
}
}
}return xmlhttp;
}function sendrequest()
{
var prov_name = document.getelementbyid("province").value;if((prov_name == null) || (prov_name == ""))
return;
xmlhttp = getxmlhttprequest();
if(xmlhttp == null)
{
alert("浏覽器不支持xmlhttprequest!");
return;
}var url = "www.bKjia.c0m.php";
url = url + "?prov=" + prov_name;xmlhttp.open("get", url, true);
xmlhttp.onreadystatechange = updatepage;
xmlhttp.send(null);
}function updatepage()
{
if(xmlhttp.readystate == 4 && xmlhttp.status == 200)
{
var response = xmlhttp.responsetext;
document.getelementbyid("city").innerhtml = response;
}
}
</script><head>
<body>
<h3>請選擇一個省(自治區):</h3><form action="www.bKjia.c0m.php">
<div>
<select id="province" onchange="sendrequest()">
<option value="">請選擇一個省(自治區)</option>
<option value="ah">安徽</option>
<option value="fj">福建</option>
<option value="gs">甘肅</option>
<option value="gd">廣東</option>
<option value="gx">廣西</option>
<option value="gz">貴州</option>
<option value="hn">海南</option>
<option value="hb">河北</option>
<option value="hh">河南</option>
<option value="hl">黑龍江</option>
</select>
</div>
</form><div id="city">
</div></body>
</html>
www.bKjia.c0m.php代碼
<?php
$city_arr = array(
"ah"=>"合肥",
"fj"=>"福州",
"gs"=>"蘭州",
"gd"=>"廣州",
"gx"=>"南寧",
"gz"=>"貴陽",
"hn"=>"海口",
"hb"=>"石家莊",
"hh"=>"鄭州",
"hl"=>"哈爾濱"
);if(empty($_get['prov']))
{
echo iconv("gb2312","utf-8",'<font color="red">您沒有選擇省(自治區)</font>');
}
else
{
$prov = $_get['prov'];
$city = $city_arr[$prov];
echo iconv("gb2312","utf-8",'所選省(自治區)省會(首府)為:'.$city);
}
?>