<?php
require 'db.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript"><!--
var xmlHttp;
function clsopt(opt)
{
if(opt)
{
//清除县/区列表内容并设置为默认值
var length=opt.length;
for(var i=length-1;i>0;i--)
{
opt.remove(i);
}
}
}
function valid(opt)
{
if(opt.options[opt.selectedIndex].value=='')
{
alert('请选择县/区');
return false;
}
}
function createXMLHttpRequest()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
var aVersions = ['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
for(var i=0; i<aVersions.length; i++)
xmlhttp = new ActiveXObject(aVersions[i]);
}
if (xmlhttp!=null)
{
return xmlhttp;
}
else
{
alert("你的浏览器不支持 XMLHTTP.");
}
}
function queryCity(opt)
{
id=opt.options[opt.selectedIndex].value;
if(id=='')
{
alert('请选择省');
clsopt(document.getElementById("areaCity"));
clsopt(document.getElementById("areaCountry"));
return false;
}
xmlHttp=createXMLHttpRequest();
type="city";
var url="getcity.php?action=city&id="+id;
xmlHttp.onreadystatechange=statechange;
xmlHttp.open("GET",url,true);
xmlHttp.send('');
}
function queryCountry(opt)
{
id=opt.options[opt.selectedIndex].value;
if(id=='')
{
alert('请选择市/区');
clsopt(document.getElementById("areaCountry"));
return false;
}
xmlHttp=createXMLHttpRequest();
type="country";
var url="getcity.php?action=area&id="+id;
xmlHttp.onreadystatechange=statechange;
xmlHttp.open("GET",url,true);
xmlHttp.send('');
}
function statechange()
{
if(xmlHttp.readyState==4&&xmlHttp.status==200)
{
if(type=="city")
{
showCity();
}
else if(type="country")
{
showCountry();
}
}
}
function showCity(){
var cityopt=document.getElementById("areaCity");
var citys=eval('('+xmlHttp.responseText+')');
//cityopt.innerHTML="";
//刷新市/区列表内容并设置为默认值
clsopt(cityopt);
var str="";
for(i in citys)
{
cityopt.options.add(new Option(citys[i].city,citys[i].cityid));
str+=citys[i].city;
}
clsopt(document.getElementById("areaCountry"));
}
function showCountry(){
var areaopt=document.getElementById("areaCountry");
//刷新县/区列表内容并设置为默认值
clsopt(areaopt);
var areas=eval('('+xmlHttp.responseText+')');
for(i in areas)
{
areaopt.options.add(new Option(areas[i].area,areas[i].areaid));
}
}
-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<div>
<span id="" class="">
省/直辖市(必选):<select name="areaProvince" id="areaProvince" onchange="queryCity(this)" >
<option value="" selected="selected" >请选择省份</option>
<?php
$rs=mysql_query("select * from province;");
?>
<? while($row=mysql_fetch_array($rs)){ ?>
<option value="<?=$row['provinceID'];?>" ><?=$row['province'];?></option>
<? };?>
</select>
</span>
<span id="" class="">
市/地区(必选):<select id="areaCity" name="city" onchange="queryCountry(this)" >
<option value="" selected="selected" >请选择市/地区</option>
</select>
</span>
<span id="" class="">
县/地区(必选):<select id="areaCountry" name="area" onchange="valid(this)" >
<option value="" selected="selected" >请选择县/地区</option>
</select>
</span>
</div>
</body>
</html>