--------------------------最开始------------------------<?php //连接数据库$conn=mysql_connect("localhost","root","123456");if(!$conn){die("数据库连接失败...".mysql_error()...
--------------------------最开始------------------------ <?php
//连接数据库
$conn = mysql_connect("localhost","root","123456");
if(!$conn){
die ("数据库连接失败...".mysql_error());
}
mysql_select_db("testajax",$conn);
mysql_query("set names 'gb2312'");
//查询语句
$sql = "select * from country_info";
$result = mysql_query($sql);
echo "国家:";
echo "请选择国家";
while($row = mysql_fetch_array($result)){
echo "---".$row['countryName']."---";
}
echo " ";
echo "";
mysql_close($conn);
?>
---------------------------script-------------------------- function getCountry(countryId){
//创建 XMLHTTP 对象
xmlHttp = GetXmlHttpObject();
if(xmlHttp == null){
alert("浏览器请求失败...");
return;
}
var url = "doCountry.php?countryId="+countryId;
//alert(url);
/* */
//xmlHttp.setRequestHeader('charset','utf-8');
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById("capital").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
//得到 xmlhttp 对象
function GetXmlHttpObject(){
var xmlHttp = null;
try{
//web 标准
xmlHttp = new XMLHttpRequest();
}catch (e){
try{
//微软的方式
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
//Internet Explorer 5.5
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
-----------------------------php--------------------------
$countryId = $_GET["countryId"];
//连接数据库
$conn = mysql_connect("localhost","root","123456");
if(!$conn){
die ("连接数据库失败...".mysql_error());
}
mysql_select_db("testajax",$conn);
mysql_query("set names 'gb2312'");
//查询语句
$sql = "select * from capital_info where countryId = '".$countryId."'";
$result = mysql_query($sql);
echo "城市:";
echo "请选择城市";
while($row = mysql_fetch_array($result)){
echo "---".$row['capitalName']."---";
}
echo "";
mysql_close($conn);
?>
展开