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

本文是一个基于asp+ajax和数据库驱动的二级联动菜单,感兴趣的同学参考下。


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
%>

 



原文地址:http://www.adminso.com/articles/view/92222
(站长搜索- http://www.adminso.com/articles -资讯,中国最具专业的站长资讯新闻频道,报道国内外动态权威的站长资讯动向,关注新闻,透视事件热点资讯。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值