|
帖出主要代码
php
<?php
require 'db.php';
?>
<?php
function getCity($fatherid){
if($fatherid)
{
$citys=array();
$rs=mysql_query("select * from city where father=$fatherid;");
while($row=mysql_fetch_array($rs))
{
$citys[]=array('cityid'=>$row['cityID'],'city'=>iconv('GBK','UTF-8',$row['city']));
}
echo json_encode($citys);
}
}
function getArea($fatherid){
if($fatherid)
{
$areas=array();
$rs=mysql_query("select * from area where father=$fatherid;");
while($row=mysql_fetch_array($rs))
{
$areas[]=array('areaid'=>$row['areaID'],'area'=>iconv('GBK','UTF-8',$row['area']));
}
echo json_encode($areas);
}
}
if(isset($_REQUEST['action'])&&$_REQUEST['action']!='')
{
switch ($_REQUEST['action'])
{
case 'city':
if(isset($_REQUEST['id'])&&$_REQUEST['id']!='')
getCity($_REQUEST['id']);
break;
case 'area':
if(isset($_REQUEST['id'])&&$_REQUEST['id']!='')
getArea($_REQUEST['id']);
break;
}
}
?>
HTML与JavaScript
<?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);
}
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.city,citys.cityid));
str+=citys.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.area,areas.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>
附件包含完整项目代码与数据库 |
|
所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298
本贴地址:https://www.yunweiku.com/thread-364200-1-1.html
上篇帖子:
MongoDB:PHP中存储和调用server side 自定义函数
下篇帖子:
html,php,asp,jsp禁止缓存的方法 (防止页面缓存的方法)
|