基于asp+ajax和数据库驱动的二级联动菜单

yipeiwu_com5年前PHP代码库
index.asp 页面代码
复制代码 代码如下:

<!--#include file="conn.asp" -->
<%
set cmd = conn.execute("select bigclassid,bigclassname from bigclass")
tempid=cmd("bigclassid")
%>
<select name="menu" onChange="getsubcategory(this.value);">

<%
if not cmd.eof then
do while not cmd.eof
bigclassid= cmd("bigclassid")
bigclassname = cmd("bigclassname")
%>
<option value="<%=bigclassid%>"><%=bigclassname%></option>
<%
cmd.movenext
loop
end if
cmd.close
set cmd = nothing
%>
</select>
<div id="subclass">
<select name="submenu">

<%
set cxd = conn.execute("select * from smallclass where bigclassid=" & tempid)
if not cxd.eof then
do while not cxd.eof
smallclassid= cxd("smallclassid")
smallclassname = cxd("smallclassname")%>
<option value="<%=smallclassid%>"><%=smallclassname%></option>
<%
cxd.movenext
loop
cxd.close
set cxd = nothing
else
html = "<select name='smallclassid'><option value='0' selected>暂无小类</option></select>"
response.write html
end if
%>
</select>
</div>

ajax.js 代码
复制代码 代码如下:

// JavaScript Document
function createxmlhttp()
{
xmlhttpobj = false;
try{
xmlhttpobj = new XMLHttpRequest;
}catch(e){
try{
xmlhttpobj=new ActiveXObject("MSXML2.XMLHTTP");
}catch(e2){
try{
xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e3){
xmlhttpobj = false;
}
}
}
return xmlhttpobj;
}

function getsubcategory(bigclassid){
if(bigclassid==0){
document.getElementById("subclass").innerHTML="<select name='smallclassid'><option value='0' selected>选择二级分类</option></select>";
return;
};
var xmlhttpobj = createxmlhttp();
if(xmlhttpobj){//如果创建对象xmlhttpobj成功
xmlhttpobj.onreadystatechange=handle;
xmlhttpobj.open('get',"getsubcategory.asp?bigclassid="+bigclassid+"&number="+Math.random(),true);//get方法 加个随机数。


xmlhttpobj.send(null);
}
}

function handle(){//客户端监控函数
//if(xmlhttpobj.readystate==4){//服务器处理请求完成
if(xmlhttpobj.status==200){
//alert('ok');
var html = xmlhttpobj.responseText;//获得返回值
document.getElementById("subclass").innerHTML=html;
}else{
document.getElementById("subclass").innerHTML="对不起,您请求的页面有问题...";
}
//}
//else{
//document.getElementById("subclass").innerHTML=xmlhttpobj.readystate;//服务器处理中
//}
//}

}

getsubcategory.asp 代码
复制代码 代码如下:

<%@language="vbscript" codepage="936"%>
<!--#include file="conn.asp"-->
<%
response.charset="gb2312"
bigclassid=safe(request.querystring("bigclassid"))
if bigclassid<>"" then
set re=new regexp
re.ignorecase=true
re.global=false
re.pattern = "^[0-9]{1,3}$"
if not re.test(bigclassid) then
response.write "非法参数"
response.end
end if%>

<%on error resume next
set p = conn.execute("select * from smallclass where bigclassid=" & bigclassid)
if err then
err.clear
response.write "查询出错"
response.end
end if
if not p.eof then
html = "<select name='select2'>"&vbnewline
do while not p.eof
html = html&"<option value='"&p("smallclassid")&"'>"&p("smallclassname")&"</option>"&vbnewline
p.movenext
loop
html = html&"</select>"
else
html = "<select name='smallclassid'><option value='0' selected>暂无小类</option></select>"
end if
p.close
set p = nothing
conn.close
set conn = nothing
response.write html
html = ""
end if
%>

相关文章

php把大写命名转换成下划线分割命名

有时候需要把一个字符串中的大写转换成 _+小写的方式,在变量命名的时候会碰到这种问题,直接上代码: $name = 'AppPromoZhongQiu2014ActiveStatus...

php组合排序简单实现方法

php组合排序简单实现方法

本文实例讲述了php组合排序简单实现方法。分享给大家供大家参考,具体如下: 今天被一个组合排序纠结了一晚上,可能是开始没转过弯,所以没想到用二个栈。用了二个栈就很简单的完成了需求效果...

PHP中如何使用Redis接管文件存储Session详解

PHP中如何使用Redis接管文件存储Session详解

前言 php默认使用文件存储session,如果并发量大,效率会非常低。而redis对高并发的支持非常好,可以利用redis替换文件来存储session。 最近就遇到了这个问题,之前找...

PHP 5.3.1 安装包 VC9 VC6不同版本的区别是什么

最近在PHP官网上看到又有新版的PHP下载了,于是上去找找 For Windows的版本,可是一看,一共给了四个版本,VC9 x86 Non Thread Safe、VC9 x86 Th...

PHPExcel导出2003和2007的excel文档功能示例

本文实例讲述了PHPExcel导出2003和2007的excel文档功能。分享给大家供大家参考,具体如下: require_once 'common/excel/PHPExcel.p...