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


Posted in PHP onMay 06, 2010

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 相关文章推荐
帖几个PHP的无限分类实现想法~
Jan 02 PHP
ThinkPHP php 框架学习笔记
Oct 30 PHP
php学习之 循环结构实现代码
Jun 09 PHP
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
May 07 PHP
PHP6 中可能会出现的新特性预览
Apr 04 PHP
php array_merge函数使用需要注意的一个问题
Mar 30 PHP
Laravel 5框架学习之Laravel入门和新建项目
Apr 07 PHP
Discuz!X中SESSION机制实例详解
Sep 23 PHP
PHP实现的简单分页类及用法示例
May 06 PHP
php7 安装yar 生成docker镜像
May 09 PHP
php简单中奖算法(实例)
Aug 15 PHP
PHP实现的迪科斯彻(Dijkstra)最短路径算法实例
Sep 16 PHP
PHP 类商品秒杀计时实现代码
May 05 #PHP
PHP 面向对象 final类与final方法
May 05 #PHP
PHP 面向对象 PHP5 中的常量
May 05 #PHP
在Windows下编译适用于PHP 5.2.12及5.2.13的eAccelerator.dll(附下载)
May 04 #PHP
一些被忽视的PHP函数(简单整理)
Apr 30 #PHP
php 将字符串按大写字母分隔成字符串数组
Apr 30 #PHP
mayfish 数据入库验证代码
Apr 30 #PHP
You might like
mysql时区问题
2008/03/26 PHP
PHP autoload与spl_autoload自动加载机制的深入理解
2013/06/05 PHP
PHP中使用break跳出多重循环代码实例
2015/01/21 PHP
php递归删除指定文件夹的方法小结
2015/04/20 PHP
php计算整个目录大小的方法
2015/06/01 PHP
PHP7.0版本备注
2015/07/23 PHP
微信公众平台开发之天气预报功能
2015/08/31 PHP
PHP排序算法之归并排序(Merging Sort)实例详解
2018/04/21 PHP
thinkPHP5框架实现多数据库连接,跨数据连接查询操作示例
2019/05/29 PHP
javascript优先加载笔记代码
2008/09/30 Javascript
javascript中负数算术右移、逻辑右移的奥秘探索
2013/10/17 Javascript
js实现动画特效的文字链接鼠标悬停提示的方法
2015/03/02 Javascript
探索Vue.js component内容实现
2016/11/03 Javascript
vue之将echart封装为组件
2018/06/02 Javascript
关于Vue源码vm.$watch()内部原理详解
2019/04/26 Javascript
js判断复选框是否选中的方法示例【基于jQuery】
2019/10/10 jQuery
如何使用JS console.log()技巧提高工作效率
2020/10/14 Javascript
[02:41]DOTA2英雄基础教程 亚巴顿
2014/01/02 DOTA
Python 模拟购物车的实例讲解
2017/09/11 Python
padas 生成excel 增加sheet表的实例
2018/12/11 Python
详解用python自制微信机器人,定时发送天气预报
2019/03/25 Python
Python第三方库face_recognition在windows上的安装过程
2019/05/03 Python
Python 保持登录状态进行接口测试的方法示例
2019/08/06 Python
解决Atom安装Hydrogen无法运行python3的问题
2019/08/28 Python
Python实现密钥密码(加解密)实例详解
2020/04/26 Python
企业业务员岗位职责
2014/03/14 职场文书
中餐厅经理岗位职责
2014/04/11 职场文书
世界读书日的活动方案
2014/08/20 职场文书
交通运输局四风问题对照检查材料思想汇报
2014/10/09 职场文书
《改造我们的学习》心得体会
2014/11/07 职场文书
解除劳动合同证明书模板
2014/11/20 职场文书
中班下学期个人总结
2015/02/12 职场文书
个人自我鉴定怎么写?
2019/07/01 职场文书
什么是检讨书?检讨书的格式及范文
2019/11/05 职场文书
Java 在线考试云平台的实现
2021/11/23 Java/Android
分享CSS盒子模型隐藏的几种方式
2022/02/28 HTML / CSS