【www.gdgbn.com--jquery】
<script type="text/网页特效" src="jquery.网页特效"></script>
<script type="text/网页特效">
$(document).ready(function() {
//get provinces
$.get("getcontent.php", {category:"province"},
function(data) {
$("#province").html(data);
});
//get citys
$.get("getcontent.php", {category:"city"},
function(data) {
$("#city").html(data);
});
//province onchange
$("#province").change(function() {
var province = $(this).val();
$.get("getcontent.php", {category:"city", province:province}, function(data) {
$("#city").html(data);
});
});
});
</script>
二级联动示例
getcontent.php代码
//author:厦门飞鱼
//qq:710788018
//email:tochenwei@163.com
define(host, "localhost");
define(user, "root");
define(pw, "");
define(db, "test");
$connect = mysql_connect(host, user, pw)
or die("could not connect to mysql server");
mysql_select_db(db,$connect)
or die("could not select database.");
//设置查询编码,不设查询时易出现乱码
mysql_query("set names utf8;");
switch($_request["category"]) {
//显示数据库中所有省份
case "province":
$str = "";
$sql = "select * from province";
$result = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
//print_r($row);
$str .= "";
}
}
echo $str;
mysql_free_result($result);
break;
//显示城市
case "city":
$str = "";
if($_request["province"] != "") {
//根据省份得到城市
$sql = "select * from city where province_id=".$_request["province"];
$result = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$str .= "";
}
}
mysql_free_result($result);
}//end of if
echo $str;
break;
}//end of switch
/*
综合上面的实例我们就可以看得,其实这就是php+ajax实例的无刷新二级菜单了,只是使用的方法不一样罢了。
*/
?>
city.sql
use `test`;
/*table structure for table `city` */
drop table if exists `city`;
create table `city` (
`id` int(4) not null auto_increment,
`city_name` varchar(255) not null,
`province_id` int(4) not null,
primary key (`id`)
) engine=innodb default charset=utf8;
/*data for the table `city` */
insert into `city`(`id`,`city_name`,`province_id`) values (1,"福州",1),(2,"厦门",1),(3,"龙岩",1),(4,"漳州",1),(5,"深圳",2),(6,"广州",2);
provice.sql
create database /*!32312 if not exists*/`test` /*!40100 default character set utf8 */;
use `test`;
/*table structure for table `province` */
drop table if exists `province`;
create table `province` (
`id` int(4) not null auto_increment,
`name` varchar(255) not null,
primary key (`id`)
) engine=innodb default charset=utf8;
/*data for the table `province` */
insert into `province`(`id`,`name`) values (1,"福建省"),(2,"广东省");