php四级联动

php+jQuery实现的四级联动

前台:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="jquery-1.7.1.min.js" ></script>
<script type="text/javascript" >
$.post("bak.php", {id:0},
function(status){
str = ‘<option>请选择</option>’;
for(var i=0;i<status.length;i++){
str += ‘<option value="’+status[i].id+’" >’+status[i].name+'</option>’;
}
$(‘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 = ‘<select onchange="change(this)" ><option>请选择</option>’;
for(var i=0;i<status.length;i++){
str += ‘<option value="’+status[i].id+’" >’+status[i].name+'</option>’;
}
str += ‘</select>’;
$(‘select:last’).after(str);
}
});
}
</script>
</head>
<body>
<select onchange="change(this)">

</select>
</body>
</html>

后台:


<?php

	$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

“php四级联动”的一个回复

发表评论