当选择 省后,alert 弹出的信息,如下面的图片所示。但是在城市的下拉框中还是无法显示出来。。不知道是什么原因ajax.php
<?php
INCLUDE_ONCE($GLOBALS["DOCUMENT_ROOT"]."/phplib/system.php");
include_once(__WEBPATH__."phplib/checker.php");
$HeadOption[style]="/images/css.css";
$HeadOption[body]="bgcolor=#EFEFF7";
html::head($HeadOption);
?>
<script language="javascript">
function Ajax(){
var _xmlHttp = null;
this.createXMLHttpRequest = function(){
try{
if(window.ActiveXObject){
_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
_xmlHttp = new XMLHttpRequest();
}
}
catch(e){
alert(e.name +" : " + e.message);
}
}
this.backFunction = function(_backFunction){
if(_xmlHttp.readyState == 4){
if(_xmlHttp.status == 200){
_backFunction(_xmlHttp.responseText);
}
}
_xmlHttp.onreadystatechange = null;
}
this.doPost = function(_url,_parameter,_backFunction){
try{_xmlHttp.open("POST",_url, false);
_xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
_xmlHttp.send(_parameter);
}
catch(e){
alert(e.name +" : " + e.message);
}
}
this.doGet = function(_url,_parameter,_backFunction){
try{
var _random = Math.round(Math.random()*10000);
_xmlHttp.open("GET", (_url+"?random=" +_random +"&" + _parameter), false);
_xmlHttp.send(null);
}
catch(e){
alert(e.name +" : " + e.message);
}
}
this.ajaxRequest = function(_url,_parameter,_method,_backFunction){
try{
this.createXMLHttpRequest();
if(_method.toLowerCase() == "post"){
this.doPost(_url,_parameter,_backFunction);
}
else{
this.doGet(_url,_parameter,_backFunction);
}
try{
_xmlHttp.onreadystatechange = this.backFunction(_backFunction);
}
catch(err){}
}
catch(e){
alert(e.name+" : "+e.message);
}
}
}function getCity(Id){
var url="select.php";
var parameter="province="+Id;
var method="post";
new Ajax().ajaxRequest(url,parameter,method,callBack);
function callBack(xml){
alert(unescape(xml));
$('shi').innerHTML=citys;
$('city1').innerHTML=citys;
}
}
function $(id){
return document.getElementById(id);
}
</script><body><form name="myform">
<table>
<tr>
<td>
<?php
echo "<select name='provinceid' id='sheng' onChange='getCity(this.value)'>";
$sqluser="select * from provinced";
$MYSQL->Query($sqluser);
for($i=0;$i<$MYSQL->rows;$i++){
$MYSQL->Fetch($i);
print "<option value='".$MYSQL->data[provinceid]."'>".$MYSQL->data[name]."</option>";
}
echo "</select>";
?>
</td>
<td><div id="city1"></div>
<select name="city" id="shi">
<option value="">选择城市</option>
</select>
</td>
</tr>
</table>
</form></body><?
// 每页结尾查询
include_once(__WEBPATH__."htmllib/adminPageFoot.php");
include_once(__WEBPATH__."phplib/foot.php");
?>select.php
<?php
header("Content-Type: text/xml;charset=GB2312");
header("Cache-Control:no-cache");
INCLUDE_ONCE($GLOBALS["DOCUMENT_ROOT"]."/phplib/system.php");
include_once(__WEBPATH__."phplib/checker.php");
$HeadOption[style]="/images/css.css";
$HeadOption[body]="bgcolor=#EFEFF7";
html::head($HeadOption);
OB_Start();
OB_Clean(); $province=trim($_POST['province']);
$sql="select * from city where provinceid=".$province." ";
echo $sql;
$MYSQL->Query($sql);
$citys="<select>";
for($i=0;$i<$MYSQL->rows;$i++){
$MYSQL->Fetch($i);
print $citys="<option value='".$MYSQL->data[cityid]."'>".$MYSQL->data[name]."</option>";
}
$citys="</select>";
//echo $citys;
//die();
?><?
// 每页结尾查询
include_once(__WEBPATH__."htmllib/adminPageFoot.php");
include_once(__WEBPATH__."phplib/foot.php");
?>
PHP
<?php
INCLUDE_ONCE($GLOBALS["DOCUMENT_ROOT"]."/phplib/system.php");
include_once(__WEBPATH__."phplib/checker.php");
$HeadOption[style]="/images/css.css";
$HeadOption[body]="bgcolor=#EFEFF7";
html::head($HeadOption);
?>
<script language="javascript">
function Ajax(){
var _xmlHttp = null;
this.createXMLHttpRequest = function(){
try{
if(window.ActiveXObject){
_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
_xmlHttp = new XMLHttpRequest();
}
}
catch(e){
alert(e.name +" : " + e.message);
}
}
this.backFunction = function(_backFunction){
if(_xmlHttp.readyState == 4){
if(_xmlHttp.status == 200){
_backFunction(_xmlHttp.responseText);
}
}
_xmlHttp.onreadystatechange = null;
}
this.doPost = function(_url,_parameter,_backFunction){
try{_xmlHttp.open("POST",_url, false);
_xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
_xmlHttp.send(_parameter);
}
catch(e){
alert(e.name +" : " + e.message);
}
}
this.doGet = function(_url,_parameter,_backFunction){
try{
var _random = Math.round(Math.random()*10000);
_xmlHttp.open("GET", (_url+"?random=" +_random +"&" + _parameter), false);
_xmlHttp.send(null);
}
catch(e){
alert(e.name +" : " + e.message);
}
}
this.ajaxRequest = function(_url,_parameter,_method,_backFunction){
try{
this.createXMLHttpRequest();
if(_method.toLowerCase() == "post"){
this.doPost(_url,_parameter,_backFunction);
}
else{
this.doGet(_url,_parameter,_backFunction);
}
try{
_xmlHttp.onreadystatechange = this.backFunction(_backFunction);
}
catch(err){}
}
catch(e){
alert(e.name+" : "+e.message);
}
}
}function getCity(Id){
var url="select.php";
var parameter="province="+Id;
var method="post";
new Ajax().ajaxRequest(url,parameter,method,callBack);
function callBack(xml){
alert(unescape(xml));
$('shi').innerHTML=citys;
$('city1').innerHTML=citys;
}
}
function $(id){
return document.getElementById(id);
}
</script><body><form name="myform">
<table>
<tr>
<td>
<?php
echo "<select name='provinceid' id='sheng' onChange='getCity(this.value)'>";
$sqluser="select * from provinced";
$MYSQL->Query($sqluser);
for($i=0;$i<$MYSQL->rows;$i++){
$MYSQL->Fetch($i);
print "<option value='".$MYSQL->data[provinceid]."'>".$MYSQL->data[name]."</option>";
}
echo "</select>";
?>
</td>
<td><div id="city1"></div>
<select name="city" id="shi">
<option value="">选择城市</option>
</select>
</td>
</tr>
</table>
</form></body><?
// 每页结尾查询
include_once(__WEBPATH__."htmllib/adminPageFoot.php");
include_once(__WEBPATH__."phplib/foot.php");
?>select.php
<?php
header("Content-Type: text/xml;charset=GB2312");
header("Cache-Control:no-cache");
INCLUDE_ONCE($GLOBALS["DOCUMENT_ROOT"]."/phplib/system.php");
include_once(__WEBPATH__."phplib/checker.php");
$HeadOption[style]="/images/css.css";
$HeadOption[body]="bgcolor=#EFEFF7";
html::head($HeadOption);
OB_Start();
OB_Clean(); $province=trim($_POST['province']);
$sql="select * from city where provinceid=".$province." ";
echo $sql;
$MYSQL->Query($sql);
$citys="<select>";
for($i=0;$i<$MYSQL->rows;$i++){
$MYSQL->Fetch($i);
print $citys="<option value='".$MYSQL->data[cityid]."'>".$MYSQL->data[name]."</option>";
}
$citys="</select>";
//echo $citys;
//die();
?><?
// 每页结尾查询
include_once(__WEBPATH__."htmllib/adminPageFoot.php");
include_once(__WEBPATH__."phplib/foot.php");
?>
PHP
1.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <script type="text/javascript" src="jquery-1.7.js"></script> <script type="text/javascript">
function ajaxSelectArea(cityID)
{
$.ajax({ type:"GET", url:"2.php", data:{city:cityID}, success:function(response){ if(response != ''){ $('select[name=area]').html(response);//生成的市信息 $('select[name=area]').show();//显示市信息 } } }) }
</script> </head> <body> <div align="center" style="margin-top:200px;"> <?php $conn = mysql_connect("localhost", "root", "123456"); mysql_select_db("jx_gc"); mysql_query("set names 'GBK'"); $sql = "select * from provinced"; $result = mysql_query($sql); echo "<from id='droplist'>\n"; echo "<select name='province'>\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 "<select name='selectCity'></select>\n"; echo "<select name='area'></select>\n"; echo "</form>\n"; ?> </div> </body> </html>2.php
<?php $conn=mysql_connect("localhost","root","123456"); mysql_select_db("jx_gc"); mysql_query("set names 'utf8'"); $provincecode = $_GET['province'];//获取js传递过来的省份的id //如果选择的省份不是直辖市和新疆,台湾,香港,澳门 if($provincecode !="" && $provincecode !=650000 && $provincecode != 710000 && $provincecode != 810000 && $provincecode != 820000 && $provincecode != 110000 && $provincecode !=120000 && $provincecode != 310000 && $provincecode != 500000){ $sql="select * from city where provinceid=$provincecode"; $result=mysql_query($sql); $city = ''; $city .= "<option value='-1' selected>请选择城市</option>\n"; while($row=mysql_fetch_row($result)){ $city .= "<option value='$row[1]'>$row[2]</option>\n"; } echo $city; } $citycode = $_GET['city']; if($citycode != ""){ $sql="select * from county where cityid=$citycode"; $result=mysql_query($sql); $area = ''; $area .= "<option value='-1' selected>请选择县</option>\n"; while($row=mysql_fetch_row($result)){ $area .= "<option value='$row[1]'>$row[2]</option>\n"; } echo $area; } ?>
for($i=0;$i<$MYSQL->rows;$i++){
$MYSQL->Fetch($i);
$citys .= "<option value='".$MYSQL->data[cityid]."'>".$MYSQL->data[name]."</option>";
}
$citys .= "</select>";
echo $citys;