<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<select id="sheng" οnchange="getArea(this.value,'shi')" ></select> 省 <select id="shi" οnchange="getArea(this.value,'xian')" ></select> 市 <select id="xian" ></select>
</body>
</html>
<script language="javascript" type="text/javascript">
function getArea(id,p){
//初始化ajax
var xhr = new XMLHttpRequest();
var url = "./area-html2.php?id="+id+"&r="+Math.random();
var sel=document.getElementById(p);
//打开请求
xhr.open("get",url,true);
//发送数据
xhr.send(null);
//等待响应
xhr.onreadystatechange = function (){
if(xhr.readyState == 4){
var arr1=xhr.responseText.split(";");
//清空下拉菜单
sel.length=0;
for(var i=0;i<arr1.length;i++){
var arr2=arr1[i].split(":");
//产生一个option对象
var opt=new Option(arr2[1],arr2[0]);
//添加到当前列表当中
sel.add(opt,null);
}
}
}
//响应的结果直接放到对应的下拉菜单中
}
//加载所有的省份
getArea("",'sheng')
</script>
area-html2.php
<?php
header("Content-Type:text/html;charset=UTF-8");
//连接数据库
//include("../include/dbconn.php");
mysql_connect("localhost","root","liuhaichuang");
mysql_select_db("test");
mysql_query("set names 'utf8'");
//根据用户返回的ID查询
$id=$_GET['id'];
//echo $id;
$sql="select id,name from area where id like '".$id."__'";
$rs=mysql_query($sql);
echo $rs;
//将查询的结果封装成html标记
//echo mysql_num_rows($rs);
//echo "<option value='0'>请选择</option>";
echo ":";
while($rows=mysql_fetch_assoc($rs)){
echo ';'.$rows['id'].':'.$rows['name'];
}
?>
INSERT INTO `area` VALUES ('11', '北京市', 'Beijing Shi', '110000', 'bj', 0);
INSERT INTO `area` VALUES ('12', '天津市', 'Tianjin Shi', '120000', 'TJ', 0);
INSERT INTO `area` VALUES ('13', '河北省', 'Hebei Sheng', '130000', 'HE', 0);
INSERT INTO `area` VALUES ('14', '山西省', 'Shanxi Sheng', '140000', 'SX', 0);
INSERT INTO `area` VALUES ('15', '内蒙古自治区', 'Nei Mongol Zizhiqu', '150000', 'NM', 0);
INSERT INTO `area` VALUES ('21', '辽宁省', 'Liaoning Sheng', '210000', 'LN', 0);
INSERT INTO `area` VALUES ('22', '吉林省', 'Jilin Sheng', '220000', 'JL', 0);
INSERT INTO `area` VALUES ('23', '黑龙江省', 'Heilongjiang Sheng', '230000', 'HL', 0);
INSERT INTO `area` VALUES ('31', '上海市', 'Shanghai Shi', '310000', 'sh', 0);
INSERT INTO `area` VALUES ('32', '江苏省', 'Jiangsu Sheng', '320000', 'JS', 0);
INSERT INTO `area` VALUES ('33', '浙江省', 'Zhejiang Sheng', '330000', 'ZJ', 0);
INSERT INTO `area` VALUES ('34', '安徽省', 'Anhui Sheng', '340000', 'AH', 0);
INSERT INTO `area` VALUES ('35', '福建省', 'Fujian Sheng', '350000', 'FJ', 0);
INSERT INTO `area` VALUES ('36', '江西省', 'Jiangxi Sheng', '360000', 'JX', 0);
INSERT INTO `area` VALUES ('37', '山东省', 'Shandong Sheng', '370000', 'SD', 0);
INSERT INTO `area` VALUES ('41', '河南省', 'Henan Sheng', '410000', 'HA', 0);
INSERT INTO `area` VALUES ('42', '湖北省', 'Hubei Sheng', '420000', 'HB', 0);
INSERT INTO `area` VALUES ('43', '湖南省', 'Hunan Sheng', '430000', 'HN', 0);
INSERT INTO `area` VALUES ('44', '广东省', 'Guangdong Sheng', '440000', 'GD', 0);
INSERT INTO `area` VALUES ('45', '广西壮族自治区', 'Guangxi Zhuangzu Zizhiqu', '450000', 'GX', 0);
INSERT INTO `area` VALUES ('46', '海南省', 'Hainan Sheng', '460000', 'HI', 0);
INSERT INTO `area` VALUES ('50', '重庆市', 'Chongqing Shi', '500000', 'CQ', 0);
INSERT INTO `area` VALUES ('51', '四川省', 'Sichuan Sheng', '510000', 'SC', 0);
INSERT INTO `area` VALUES ('52', '贵州省', 'Guizhou Sheng', '520000', 'GZ', 0);
INSERT INTO `area` VALUES ('53', '云南省', 'Yunnan Sheng', '530000', 'YN', 0);
INSERT INTO `area` VALUES ('54', '西藏自治区', 'Xizang Zizhiqu', '540000', 'XZ', 0);
INSERT INTO `area` VALUES ('61', '陕西省', 'Shanxi Sheng', '610000', 'SN', 0);
INSERT INTO `area` VALUES ('62', '甘肃省', 'Gansu Sheng', '620000', 'GS', 0);
INSERT INTO `area` VALUES ('63', '青海省', 'Qinghai Sheng', '630000', 'QH', 0);
INSERT INTO `area` VALUES ('64', '宁夏回族自治区', 'Ningxia Huizu Zizhiqu', '640000', 'NX', 0);
INSERT INTO `area` VALUES ('65', '新疆维吾尔自治区', 'Xinjiang Uygur Zizhiqu', '650000', 'XJ', 0);
INSERT INTO `area` VALUES ('71', '台湾省', 'Taiwan Sheng', '710000', 'TW', 0);
INSERT INTO `area` VALUES ('81', '香港特别行政区', 'Hongkong Tebiexingzhengqu', '810000', 'HK', 0);
INSERT INTO `area` VALUES ('110101', '东城区', 'Dongcheng Qu', '110101', 'DCQ', 0);
INSERT INTO `area` VALUES ('110102', '西城区', 'Xicheng Qu', '110102', 'XCQ', 0);
INSERT INTO `area` VALUES ('110103', '崇文区', 'Chongwen Qu', '110103', 'CWQ', 0);
INSERT INTO `area` VALUES ('110104', '宣武区', 'Xuanwu Qu', '110104', 'XWQ', 0);
INSERT INTO `area` VALUES ('110105', '朝阳区', 'Chaoyang Qu', '110105', 'CYQ', 0);
INSERT INTO `area` VALUES ('110106', '丰台区', 'Fengtai Qu', '110106', 'FTQ', 0);
INSERT INTO `area` VALUES ('110107', '石景山区', 'Shijingshan Qu', '110107', 'SJS', 0);
INSERT INTO `area` VALUES ('110108', '海淀区', 'Haidian Qu', '110108', 'HDN', 0);
INSERT INTO `area` VALUES ('110109', '门头沟区', 'Mentougou Qu', '110109', 'MTG', 0);
INSERT INTO `area` VALUES ('110111', '房山区', 'Fangshan Qu', '110111', 'FSQ', 0);
INSERT INTO `area` VALUES ('110112', '通州区', 'Tongzhou Qu', '110112', 'TZQ', 0);
INSERT INTO `area` VALUES ('110113', '顺义区', 'Shunyi Qu', '110113', 'SYI', 0);
INSERT INTO `area` VALUES ('1101', '北京市', 'Beijing Shi', '110100', 'bj', 1);
INSERT INTO `area` VALUES ('110114', '昌平区', 'Changping Qu', '110114', 'CP Q', 0);
INSERT INTO `area` VALUES ('110115', '大兴区', 'Daxing Qu', '110115', 'DX Q', 0);
INSERT INTO `area` VALUES ('110117', '平谷区', 'Pinggu Qu', '110117', 'PG Q', 0);
INSERT INTO `area` VALUES ('110116', '怀柔区', 'Huairou Qu', '110116', 'HR Q', 0);
INSERT INTO `area` VALUES ('110228', '密云县', 'Miyun Xian', '110228', 'MYN', 0);
INSERT INTO `area` VALUES ('110229', '延庆县', 'Yanqing Xian', '110229', 'YQX', 0);
INSERT INTO `area` VALUES ('120101', '和平区', 'Heping Qu', '120101', 'HPG', 0);
INSERT INTO `area` VALUES ('120102', '河东区', 'Hedong Qu', '120102', 'HDQ', 0);
INSERT INTO `area` VALUES ('120103', '河西区', 'Hexi Qu', '120103', 'HXQ', 0);
INSERT INTO `area` VALUES ('120104', '南开区', 'Nankai Qu', '120104', 'NKQ', 0);
INSERT INTO `area` VALUES ('120105', '河北区', 'Hebei Qu', '120105', 'HBQ', 0);
INSERT INTO `area` VALUES ('120106', '红桥区', 'Hongqiao Qu', '120106', 'HQO', 0);
INSERT INTO `area` VALUES ('120107', '塘沽区', 'Tanggu Qu', '120107', 'TGA', 0);
INSERT INTO `area` VALUES ('120108', '汉沽区', 'Hangu Qu', '120108', 'HGQ', 0);
INSERT INTO `area` VALUES ('120109', '大港区', 'Dagang Qu', '120109', 'DGJ', 0);
INSERT INTO `area` VALUES ('120110', '东丽区', 'Dongli Qu', '120110', 'DLI', 0);
INSERT INTO `area` VALUES ('120111', '西青区', 'Xiqing Qu', '120111', 'XQG', 0);
INSERT INTO `area` VALUES ('120112', '津南区', 'Jinnan Qu', '120112', 'JNQ', 0);
INSERT INTO `area` VALUES ('120113', '北辰区', 'Beichen Qu', '120113', 'BCQ', 0);
INSERT INTO `area` VALUES ('1201', '天津市', 'Xian', '120100', 'tj', 1);
INSERT INTO `area` VALUES ('120221', '宁河县', 'Ninghe Xian', '120221', 'NHE', 0);
INSERT INTO `area` VALUES ('120114', '武清区', 'Wuqing Qu', '120114', 'WQ Q', 0);
INSERT INTO `area` VALUES ('120223', '静海县', 'Jinghai Xian', '120223', 'JHT', 0);
INSERT INTO `area` VALUES ('120115', '宝坻区', 'Baodi Qu', '120115', 'BDI', 0);
INSERT INTO `area` VALUES ('120225', '蓟县', 'Ji Xian', '120225', 'JIT', 0);
INSERT INTO `area` VALUES ('1301', '石家庄市', 'Shijiazhuang Shi', '130100', 'SJW', 0);
INSERT INTO `area` VALUES ('130102', '长安区', 'Chang,an Qu', '130102', 'CAQ', 0);
INSERT INTO `area` VALUES ('130103', '桥东区', 'Qiaodong Qu', '130103', 'QDQ', 0);
INSERT INTO `area` VALUES ('130104', '桥西区', 'Qiaoxi Qu', '130104', 'QXQ', 0);
INSERT INTO `area` VALUES ('130105', '新华区', 'Xinhua Qu', '130105', 'XHK', 0);
INSERT INTO `area` VALUES ('130106', '郊区', 'Jiaoqu', '130106', 'JQS', 0);
INSERT INTO `area` VALUES ('130107', '井陉矿区', 'Jingxing Kuangqu', '130107', 'JXK', 0);
INSERT INTO `area` VALUES ('130121', '井陉县', 'Jingxing Xian', '130121', 'JXJ', 0);
INSERT INTO `area` VALUES ('130123', '正定县', 'Zhengding Xian', '130123', 'ZDJ', 0);
INSERT INTO `area` VALUES ('130124', '栾城县', 'Luancheng Xian', '130124', 'LCG', 0);
INSERT INTO `area` VALUES ('130125', '行唐县', 'Xingtang Xian', '130125', 'XTG', 0);
INSERT INTO `area` VALUES ('130126', '灵寿县', 'Lingshou Xian', '130126', 'LSO', 0);
INSERT INTO `area` VALUES ('130127', '高邑县', 'Gaoyi Xian', '130127', 'GYJ', 0);