实现三级联动菜单(ajax)

前端之家收集整理的这篇文章主要介绍了实现三级联动菜单(ajax)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如何实现三级联动菜单

首先先建一个全国各省市县数据库(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 onload="getArea('','t_province')">
<select id="t_province" onchange="getArea(this.value,'t_city')"></select>省
<select id="t_city" onchange="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>";
}
}

原文链接:https://www.f2er.com/ajax/166658.html

猜你在找的Ajax相关文章