数据库
.
.
test.html
..
<html>
<head><title>php_ajax联动下拉菜单</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script language="javascript" src="ajax.js"></script>
<script language="javascript">
function query(){
var pid = document.getElementByIdx("bigClassName").value;
if(pid!=""){
createRequest('query.php?pid='+pid);
}
}
</script>
</head>
<body>
<form action="<?=$PHP_SELF?>?" name="class" method="get" >
<select name="bigClassName" id="bigClassName" onchange="query()">
<option value="">--选择大类--</option>
<?php
$link=mysql_connect("localhost","root","root");
mysql_select_db("sortclass") or die("没有此数据库!");
$sql="select cid,cname from class where pid=0";
mysql_query("set names gb2312");
$res=mysql_query($sql,$link);
while($result=mysql_fetch_assoc($res)){
echo "<option value='".$result["cid"]."'>".$result["cname"]."</option>";
}
?>
</select>
<select name="smallClassName" id="smallClassName">
<option value="">--选择小类--</option>
</select>
<input type="submit" value="提交" />
</form>
</body>
</html>
.
ajax.js
.
var http_request =
false;
function createRequest(url) {
//初始化对象并发出XMLHttpRequest请求
http_request =
false;
if (window.XMLHttpRequest) {
//Mozilla等其他浏览器
http_request =
new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) {
//IE浏览器
try {
http_request =
new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
http_request =
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!http_request) {
alert("不能创建XMLHTTP实例!");
return false;
}
http_request.onreadystatechange = alertContents;
//指定响应方法
http_request.open("GET", url,
true);
//发出HTTP请求
http_request.send(
null);
}
function alertContents() {
//处理服务器返回的信息
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var smallClassName=document.getElementByIdx("smallClassName");
var dataArray=[];
//alert(http_request.responseText);
myVariable=http_request.responseText;
//形如: 1,新闻中心;2,学习园地;
var stringArray = myVariable.split(";");
//以;分隔字符串
stringArray.pop();
//移除数组最后一个元素,stringArray[0]==1,新闻中心 stringArray[1]==2,学习园地
var len=stringArray.length;
for(var i=0;i<len;i++){
dataArray[i]= stringArray[i].split(",");
// 循环数据条数按,分割字符串
}
//alert(dataArray[1][0]);//返回 新闻中心
//初始化smallClassName的数据
smallClassName.length=0;
var alertOption=document.createElement_x("OPTION");
alertOption.value="";
alertOption.text="--选择小类--";
smallClassName.add(alertOption);
for(var j=0;j<len;j++){
//添加数据
var objOption=document.createElement_x("OPTION");
objOption.value = dataArray[j][0];
objOption.text = dataArray[j][1];
smallClassName.add(objOption);
}
}
else {
alert('您请求的页面发现错误');
}
}
}
..
query.php
..
<?php
error_reporting(E_ERROR|E_WARNING|E_PARSE);
$link=mysql_connect("localhost","root","root");
mysql_select_db("sortclass",$link) or die("no such database!");
//$GB2312string=iconv( 'UTF-8', 'gb2312//IGNORE' , $RequestAjaxString); //Ajax中先用encodeURIComponent对要提交的中文进行编码
mysql_query("set names gb2312");
$pid=$_GET[pid];
$res=mysql_query("select cid,cname from class where pid='$pid'",$link);
header('Content-type: text/html;charset=GB2312');
//指定发送数据的编码格式为GB2312
while($info=mysql_fetch_array($res)){
$str.=$info["cid"].",".$info["cname"];
$str.=";";
}
echo $str;
?>