正文
字体大小: 中
PHP+Ajax实现二级联动下拉菜单!
(-12-05 01:11:39)
标签:
杂谈
数据库
...............................................................................................................................
...............................................................................................................................
test.html
................................................................................................................................
php_ajax联动下拉菜单
content="text/html;charset=gb2312">
function query(){
var pid = document.getElementByIdx("bigClassName").value;
if(pid!=""){
createRequest('query.php?pid='+pid);
}
}
action="=$PHP_SELF?>?"
method="get" >
οnchange="query()">
value="">--选择大类--
$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 "
value='".$result["cid"]."'>".$result["cname"]."
";
}
?>
value="">--选择小类--
/>
........................................................................................................................................
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
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
var objOption=document.createElement_x("OPTION");
objOption.value = dataArray[j][0];
objOption.text = dataArray[j][1];
smallClassName.add(objOption);
}
} else {
alert('您请求的页面发现错误');
}
}
}
...................................................................................................................................
query.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;
?>
.............................................................................................................................................
分享:
喜欢
阅读┊
┊
┊┊
┊打印┊
已投稿到:
文章转载请注明来源。新辉网主页 > 网页基础 > Javascript/Ajax >
标题:PHP+Ajax实现二级联动下拉菜单!
地址:/a/9422.html
关键词:PHP+Ajax实现二级联动下拉菜单!_zhuwujiasi