如何实现三级联动菜单?
首先先建一个全国各省市县数据库(http://download.csdn.net/detail/feilong_12/5141762)这里有现成的数据库直接考过来就可以用而且比较全面的哦。
下面是:三级联动的html文件(里面写ajax)
<html>
<head>
<script>
function getArea(val,table){
var xhr;
if(window.ActiveXObject){
xhr=new ActiveXOject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
var url="ajax.php";
xhr.open("post",url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("val="+val+"&table="+table);
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
//alert(xhr.responseText);
document.getElementById(table).innerHTML=xhr.responseText;
}
}
}
}
</script>
</head>
<body οnlοad="getArea('','t_province')">
<select id="t_province" οnchange="getArea(this.value,'t_city')"></select>省
<select id="t_city" οnchange="getArea(this.value,'t_district')"></select>市
<select id="t_district"></select>县
</body>
</html>
ajax.php文件
<?php
mysql_connect('localhost','root','123');
mysql_select_db('china');
mysql_query("set names utf8");
$val=$_POST['val'];
$table=$_POST['table'];
if($table=='t_province'){
$sql="select ProName from $table order by ProSort";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}
}else if($table=='t_city'){
$sql="select CityName from t_city where ProId=(select ProID from t_province where ProName='$val')";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}var_dump($rows);
}
else if($table=='t_district'){
$sql="select DisName from t_district where CityId=(select CityID from t_city where CityName='$val')";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}
}