楼主已经纠结了一个星期了,现在只能连接到省和市,县完全无反应。求各位大大帮帮忙啊,顺便求一个省市县的完整数据库!!!
不要其他的连接,我只想知道我的这段代码哪里错了~~~~
conn.php
<?php$conn=mysql_connect("localhost","root","");
mysql_select_db("test2",$conn);
mysql_query("set names 'utf8'");
mysql_query("set character set 'utf8'");
?>
index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<select id="state" name="state" onChange="getcity()">
<option value="" selected>省</option>
<?php
include("conn.php");
$sql="select * from state";
$query=mysql_query($sql);
while ($rows=mysql_fetch_array($query)){?>
<option value="<?php echo $rows['state_id'];?>"><?php echo $rows['state_name'];?></option>
<?php }?>
</select>
<select name="city" id="city" onChange="gettown()">
<option value="" selected>市</option>
</select>
<select name="town" id="town">
<option value="" selected>县</option>
</select>
</body>
</html>ajax_city.php<?php
include("conn.php");
$stateid=$_GET['state_id'];
$sql="select * from city where state_id=$stateid";
$querys=mysql_query($sql);?>
<select name="city">
<?php
while($rows=mysql_fetch_array($querys)){?>
<option value="<?php echo $rows['city_id']?>"><?php echo $rows['city_name']?></option>
<?php }?></select>ajax_town.php<?php
include("conn.php");
$cityid=$_GET['city_id'];
$sql="select * from town where city_id=$cityid";
$querys=mysql_query($sql);?>
<select name="town">
<?php
while($rows=mysql_fetch_array($querys)){?>
<option value="<?php echo $rows['town_id']?>"><?php echo $rows['town_name']?></option>
<?php }?></select>
ajax.phpvar xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
try {
xmlHttp = new ActiveXObject(XmlHttpVersions);
} catch (e) {}
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}function getcity(){
//document.getElementById(ID)
var selectObj = document.getElementById("state");
var url = "ajax_city.php?state_id=" +escape(selectObj.options[selectObj.selectedIndex].value);
//open("method","url")
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = getcityOk;
xmlHttp.send(null);
}function getcityOk(){
if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){
var response = xmlHttp.responseText;
try{
var classObj = document.getElementById("city");
classObj.outerHTML=response;
}
catch(exception){}
}
}
}function gettown(){
var selectObj = document.getElementById("city");
var url = "ajax_town.php?city_id=" +escape(selectObj.options[selectObj.selectedIndex].value);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = gettownOk;
xmlHttp.send(null);
}function gettownOk(){
if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){
var response = xmlHttp.responseText;
try{
var classObj = document.getElementById("town"); classObj.outerHTML=response;
}
catch(exception){}
}
}
}
不要其他的连接,我只想知道我的这段代码哪里错了~~~~
conn.php
<?php$conn=mysql_connect("localhost","root","");
mysql_select_db("test2",$conn);
mysql_query("set names 'utf8'");
mysql_query("set character set 'utf8'");
?>
index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<select id="state" name="state" onChange="getcity()">
<option value="" selected>省</option>
<?php
include("conn.php");
$sql="select * from state";
$query=mysql_query($sql);
while ($rows=mysql_fetch_array($query)){?>
<option value="<?php echo $rows['state_id'];?>"><?php echo $rows['state_name'];?></option>
<?php }?>
</select>
<select name="city" id="city" onChange="gettown()">
<option value="" selected>市</option>
</select>
<select name="town" id="town">
<option value="" selected>县</option>
</select>
</body>
</html>ajax_city.php<?php
include("conn.php");
$stateid=$_GET['state_id'];
$sql="select * from city where state_id=$stateid";
$querys=mysql_query($sql);?>
<select name="city">
<?php
while($rows=mysql_fetch_array($querys)){?>
<option value="<?php echo $rows['city_id']?>"><?php echo $rows['city_name']?></option>
<?php }?></select>ajax_town.php<?php
include("conn.php");
$cityid=$_GET['city_id'];
$sql="select * from town where city_id=$cityid";
$querys=mysql_query($sql);?>
<select name="town">
<?php
while($rows=mysql_fetch_array($querys)){?>
<option value="<?php echo $rows['town_id']?>"><?php echo $rows['town_name']?></option>
<?php }?></select>
ajax.phpvar xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
try {
xmlHttp = new ActiveXObject(XmlHttpVersions);
} catch (e) {}
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}function getcity(){
//document.getElementById(ID)
var selectObj = document.getElementById("state");
var url = "ajax_city.php?state_id=" +escape(selectObj.options[selectObj.selectedIndex].value);
//open("method","url")
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = getcityOk;
xmlHttp.send(null);
}function getcityOk(){
if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){
var response = xmlHttp.responseText;
try{
var classObj = document.getElementById("city");
classObj.outerHTML=response;
}
catch(exception){}
}
}
}function gettown(){
var selectObj = document.getElementById("city");
var url = "ajax_town.php?city_id=" +escape(selectObj.options[selectObj.selectedIndex].value);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = gettownOk;
xmlHttp.send(null);
}function gettownOk(){
if (xmlHttp.readyState == 4) {
if(xmlHttp.status==200){
var response = xmlHttp.responseText;
try{
var classObj = document.getElementById("town"); classObj.outerHTML=response;
}
catch(exception){}
}
}
}
解决方案 »
- PHP操作数据库问题
- 请人写一个可以写CONFIG。php的文件和JS文件的程序
- 听说 PHP 的 $_REQUEST 可以读取 $_SESSION ?
- Global类成员变量的错误
- javascript 中奇怪的数据转换问题,求教高人???
- 请教大家一个问题!关于搜索的
- 在linux+apache+php环境下,如何配置php的openssl模块?
- 大家好,who 用过strval这个函数,我这么用怎么不行呀?
- 请问哪里有大量的php原码下载?
- php-sockets.dll中的这一组函数如Socket等都不能用,怎么办?
- mysql删除记录后,添加记录调过删除id.如何重新排序.
- 关于搜索数据库的建设问题
mysql_select_db("test2",$conn);
mysql_query("set names 'utf8'");
mysql_query("set character set 'utf8'");
就拿这段说吧,单双引号就不说了,就说后两句就不知道什么意思了,还有编码类型不要加引号
<script>
//获取指定省的市的ajax和js操作
function getCity(){
}
//获取指定市的县的ajax和js操作
function getTown(){
}
</script>
<select onchange="getCity()">省</select>
<select id="city" onchange="getTown()">市</select>
<select id="town">县</select>