php+ajax无刷新实现省、地、市三级联动
<select name="province" id="province" onchange="if(this.value != '')
setCity(this.options[this.selectedIndex].value);">
<option value="">-请选择-</option>
<?php教程
$sql="select * from ptypes where sid=0 and ssid = 0";
$result=mysql_query($sql,$conn) or die("返回数据记录出错!");
while($row=mysql_fetch_object($result))
{
echo ("<option
value='".$row->id."'>".$row->title."</option>");
}
?>
</select> 一级
<select name="sid" id="sid" onchange="if(this.value != '')
setssid(this.options[this.selectedIndex].value);" >
<option value="">-请选择-</option>
</select> 二级
<html>
<head>
<meta http-equiv="Content-Type" c />
<script type="text/javascript">
var xmlHttp;
var requestType="";
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function queryCity(citycode){
createXMLHttpRequest();
type="city";
var url="data.php?provincecode="+citycode;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function queryArea(citycode){
createXMLHttpRequest();
type="area";
var url="data.php?citycode="+citycode;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readystate==4){
if(xmlHttp.status==200){
if(type=="city"){
showcity();
}else if(type="area"){
showarea();
}
}
}
}
function showcity(){
document.getElementById("city").innerHTML=xmlHttp.responseText;
document.getElementById("area").innerHTML="";
}
function showarea(){
document.getElementById("area").innerHTML=xmlHttp.responseText;
}
</script>
</head>
<body>
<?
$conn=mysql_connect("localhost","root","2328725");
mysql_select_db("novel");
mysql_query("set names 'utf8'");
$sql="select * from province";
$result=mysql_query($sql);
echo "<from id='form1'>n";
echo "<select id='province' onchange='queryCity(this.options[this.selectedIndex].value)'>n";
echo "<option value='-1' selected>请选择省份</option>n";
while($row=mysql_fetch_row($result)){
echo "<option value='$row[1]'>$row[2]</option>n";
}
echo "</select>n";
echo "<span id='city'></span>n";
echo "<span id='area'></span>n";
echo "</form>n";
?>
$sql="select * from ptypes where sid = 0 and ssid =0";
$result=mysql教程_query($sql,$conn)
or
die("查询数据库教程出错1");
while($row=mysql_fetch_object($result))
{
$i=0;
$rs2="";
$sql2="select * from ptypes where sid='".$row->id."'";
$result2=mysql_query($sql2,$conn) or die("查询数据出错2");
while($row2=mysql_fetch_object($result2))
{
if ($i==0){
$t="";
}else{
$t=",";
}
$rs2.=$t."{txt:'".$row2->title."',val:'".$row2->id."'}n";
$n=0;
$rs3="";
$sql3="select * from ptypes where ssid='".$row2->id."'";
$result3=mysql_query($sql3,$conn) or die("查询是数据库出错3");
while($row3=mysql_fetch_object($result3))
{
if ($n==0)
{
$t1="";
}else{
$t1=",";
}
$rs3.=$t1."{txt:'".$row3->title."',val:'".$row3->id."'}n";
$n=1;
}
echo("sidArr['".$row2->id."']=[".$rs3."];");
$i=1;
}
echo("sidArr['".$row->id."']=[".$rs2."];");
}
?>
function setCity(province) {
removeOptions(document.getElementById('sid'));
removeOptions(document.getElementById('ssid'));
setSelectOption('sid', "", '-请选择-');
setSelectOption('ssid', "", '-请选择-');
if (!sidArr[province] || sidArr[province]==""){
document.getElementById('sid').disabled=true;
document.getElementById('ssid').disabled=true;
}else{
document.getElementById('sid').disabled=false;
document.getElementById('ssid').disabled=false;
setSelectOption('sid', sidArr[province], '-请选择-');
}
}
function setssid(province) {
removeOptions(document.getElementById('ssid'));
setSelectOption('ssid', "", '-请选择-');
if (!sidArr[province] || sidArr[province]==""){
document.getElementById('ssid').disabled=true;
}else{
document.getElementById('ssid').disabled=false;
setSelectOption('ssid', sidArr[province], '-请选择-');
}
}
</script>
</body>
</html>
<?PHP
<select name="ssid" id="ssid">
<option value="">-请选择-</option>
</select> 三级
本文由宝马娱乐在线城发布于互联网络,转载请注明出处:php+ajax无刷新实现省、地、市三级联动
关键词: