php+jQuery实现的四级联动
前台:
$.post("bak.php", {id:0},
function(status){
str = ‘请选择’;
for(var i=0;i
str += ‘’+status[i].name+'’;
}
$(‘select’).html(str);
}, "json");
function change(obj){
$(obj).nextAll().remove();
$.ajax({
‘url’:’bak.php’,
‘type’:’post’,
‘data’:{‘id’:$(obj).val()},
‘dataType’:’json’,
‘success’:function(status){
str = ‘请选择’;
for(var i=0;i
str += ‘’+status[i].name+'’;
}
str += ‘’;
$(‘select:last’).after(str);
}
});
}
后台:
$id = isset($_POST['id']) ? $_POST['id'] : 0 ;
$conn = mysql_connect('localhost','root','');
mysql_select_db('mogu');
$result = mysql_query("set names 'utf8'");
$sql = "select * from city where upid=".$id;
$result = mysql_query($sql);
while($rs = mysql_fetch_assoc($result)){
$arr[] = $rs;
}
echo json_encode($arr);
?>
数据库:
http://pan.baidu.com/share/link?shareid=475304&uk=1997302603