<!-- //procity.php --><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>AJAX 實現省市級聯</title> <script type="text/javascript">// var xmlHttp; // ���� XMLHttpRequest: function createXmlHttpRequest(){ var xmlHttp=null; try{ // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }// function getCity(pro){ xmlHttp=createXmlHttpRequest(); var url="getCity.php?p="+pro; //alert("URL is: "+url); xmlHttp.open("GET",url,true); xmlHttp.send(null); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ document.getElementById('cityList').innerHTML=xmlHttp.responseText; // var url="getTown.php?p="+pro; //alert("URL is: "+url); xmlHttp.open("GET",url,true); xmlHttp.send(null); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ document.getElementById('townList').innerHTML=xmlHttp.responseText; // Display: go(); } } } } }//function getTowns(city){ xmlHttp=createXmlHttpRequest(); var url="getTown.php?c="+city; //alert("URL is: "+url); xmlHttp.open("GET",url,true); xmlHttp.send(null); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ document.getElementById('townList').innerHTML=xmlHttp.responseText; // Display: go(); } } }// function go(){ var pro=document.getElementById('provinces').value; var city=document.getElementById('cities').value; var town=document.getElementById('towns').value; document.getElementById('info').innerHTML="你所在的地方:"+pro+"->"+city+"->"+town; }</script> <?php include ("connDB.php");
// $SQL="SELECT province FROM provinces ORDER BY province"; mysql_query("set names utf8"); $result=mysql_query($SQL) or die(mysql_error()); $rows=mysql_num_rows($result);
<?
include_once('inc/connmssql.php');
?>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<?
$qpn = "select * from B";
$rspn = mssql_query($qpn,$conn);
if(!$rspn){die('Valid result!');}
$count = 0;
while($row = mssql_fetch_array($rspn)){
?>
subcat[<?=$count?>] = new Array("<?=$row[0]?>","<?=$row[0]?>","<?=$row[1]?>");
<?
$count++;
}
echo "onecount=$count;";
?>
//联动函数
function changelocation(locationid)
{
document.myform.p_name.length = 0;
var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][2] == locationid)
{
//var newOption1=new Option(subcat[i][1], subcat[i][0]);
//document.all.p_name.add(newOption1);
document.myform.p_name.options[document.myform.p_name.length] = new Option(subcat[i][1], subcat[i][0]);
}
}
}
</script> <select name="p_type" onChange="changelocation(document.myform.p_type.options[document.myform.p_type.selectedIndex].value)" size="1">
<option selected value="">请选择</option>
<?
$qptype = 'select * from A';
$rsptype = mssql_query($qptype,$conn);
if(!$rsptype){die('Valid result!');}
while($row = mssql_fetch_array($rsptype)) {
?>
<option value="<? echo $row[0]; ?>"><? echo $row[0]; ?></option>
<? } ?>
</select> <select name="p_name">
<option selected value="">请选择</option>
</select>
经测试,这个可以用。
谢谢楼上两位。结帖了。
<!--
//procity.php
--><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX 實現省市級聯</title>
<script type="text/javascript">//
var xmlHttp; // ���� XMLHttpRequest:
function createXmlHttpRequest(){
var xmlHttp=null;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch(e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}// function getCity(pro){
xmlHttp=createXmlHttpRequest();
var url="getCity.php?p="+pro;
//alert("URL is: "+url);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById('cityList').innerHTML=xmlHttp.responseText;
//
var url="getTown.php?p="+pro;
//alert("URL is: "+url);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById('townList').innerHTML=xmlHttp.responseText;
// Display:
go();
}
}
}
}
}//function getTowns(city){
xmlHttp=createXmlHttpRequest();
var url="getTown.php?c="+city;
//alert("URL is: "+url);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById('townList').innerHTML=xmlHttp.responseText;
// Display:
go();
}
}
}//
function go(){
var pro=document.getElementById('provinces').value;
var city=document.getElementById('cities').value;
var town=document.getElementById('towns').value;
document.getElementById('info').innerHTML="你所在的地方:"+pro+"->"+city+"->"+town;
}</script>
<?php include ("connDB.php");
//
$SQL="SELECT province FROM provinces ORDER BY province";
mysql_query("set names utf8");
$result=mysql_query($SQL) or die(mysql_error());
$rows=mysql_num_rows($result);
?>
</head>
<body onLoad=getCity('北京')>
AJAX實現省市級聯<HR>
省(直轄市) <select id=provinces onChange=getCity(this.value)>
<?php
for($i=0;$i<$rows;$i++){
mysql_data_seek($result,$i);
$data=mysql_fetch_array($result);
print("<option value=$data[0]>$data[0]");
}
?>
</select>
市 <span id=cityList></span> 縣/區<span id=townList></span><BR><BR>
<div id=info></div>
</body>
</html>
<!--
//getCity.php
-->
<?php
include ("connDB.php");
header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/html;charset=utf8");
$pro=$_GET["p"];
$SQL="SELECT city FROM cities WHERE province='$pro'";
print("SQL is: ".$SQL);
mysql_query("set names utf8");
$result=mysql_query($SQL);
$rows=mysql_num_rows($result);
$cities="<select id=cities onchange=getTowns(this.value)>";
for($i=0;$i<$rows;$i++){
mysql_data_seek($result,$i);
$data=mysql_fetch_array($result);
$cities.="<option value=$data[0]>$data[0]";
}
$cities.="</select>";
echo $cities;
?><!--
//getTown.php
-->
<?php
include ("connDB.php");
header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/html;charset=utf8");
$url_string=parse_url($_SERVER["REQUEST_URI"]);
$query_string=$url_string["query"];
$parameter_string=explode('=',$query_string);
$parameter_type=$parameter_string[0];
$parameter_value=$parameter_string[1];
if($parameter_type=="p"){
$getCity_SQL="SELECT city FROM cities WHERE province='$parameter_value'limit 1";
//print("SQL is: ".$getCity_SQL);
mysql_query("set names utf8");
$result=mysql_query($getCity_SQL);
$resultset=mysql_fetch_row($result);
$rows=mysql_num_rows($result);
$defalutCity=$resultset[0];
$SQL="SELECT town FROM towns WHERE city='$defalutCity'";
}else{ $SQL="SELECT town FROM towns WHERE city='$parameter_value'";
}
//print("SQL is: ".$SQL);
mysql_query("set names utf8");
$result=mysql_query($SQL);
$rows=mysql_num_rows($result);
$cities="<select id=towns onchange=go()>";
for($i=0;$i<$rows;$i++){
mysql_data_seek($result,$i);
$data=mysql_fetch_array($result);
$cities.="<option value=$data[0]>$data[0]";
}
$cities.="</select>";
echo $cities;?>
三级联动!