基于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的超级变量$_GET获取HTML表单(Form) 数据
May 07 PHP
让Json更懂中文(JSON_UNESCAPED_UNICODE)
Oct 27 PHP
PHP中register_globals参数为OFF和ON的区别(register_globals 使用详解)
Feb 05 PHP
用Simple Excel导出xls实现方法
Dec 06 PHP
php防注入,表单提交值转义的实现详解
Jun 10 PHP
php实现文件下载实例分享
Jun 02 PHP
php中将一段数据存到一个txt文件中并显示其内容
Aug 15 PHP
修改destoon会员公司的伪静态中的com目录的方法
Aug 21 PHP
Discuz!X中SESSION机制实例详解
Sep 23 PHP
php邮件发送的两种方式
Apr 28 PHP
PHP生成图像验证码的方法小结(2种方法)
Jul 18 PHP
PHP代码覆盖率统计详解
Jul 22 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 数据同步
2006/10/09 PHP
Views rows style模板重写代码
2011/05/16 PHP
利用curl 多线程 模拟 并发的详解
2013/06/14 PHP
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
php使用APC实现实时上传进度条功能
2015/10/26 PHP
thinkphp查询,3.X 5.0方法(亲试可行)
2017/06/17 PHP
NodeJS框架Express的模板视图机制分析
2011/07/19 NodeJs
文本框根据输入内容自适应高度的代码
2011/10/24 Javascript
防止xss和sql注入:JS特殊字符过滤正则
2013/04/18 Javascript
Jquery ajax基础教程
2015/11/20 Javascript
JavaScript 七大技巧(一)
2015/12/13 Javascript
遍历json获得数据的几种方法小结
2017/01/21 Javascript
正则 js分转元带千分符号详解
2017/03/08 Javascript
深入理解vue.js中$watch的oldvalue与newValue
2017/08/07 Javascript
解决angular2 获取到的数据无法实时更新的问题
2018/08/31 Javascript
微信小程序云开发之使用云函数
2019/05/17 Javascript
vue-父子组件和ref实例详解
2019/11/10 Javascript
解决vue页面渲染但dom没渲染的操作
2020/07/27 Javascript
Python中列表和元组的相关语句和方法讲解
2015/08/20 Python
Python爬虫框架Scrapy实例代码
2018/03/04 Python
python梯度下降算法的实现
2020/02/24 Python
Python中zip函数如何使用
2020/06/04 Python
一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系
2020/07/03 Python
keras分类之二分类实例(Cat and dog)
2020/07/09 Python
浅析PyCharm 的初始设置(知道)
2020/10/12 Python
详解vscode实现远程linux服务器上Python开发
2020/11/10 Python
java关于string最常出现的面试题整理
2021/01/18 Python
Mistine官方海外旗舰店:泰国国民彩妆品牌
2016/12/28 全球购物
网上常见的一份Linux面试题(多项选择部分)
2015/02/07 面试题
班组长工作职责
2013/12/25 职场文书
大学生毕业的自我评价分享
2014/01/02 职场文书
《分一分》教学反思
2014/04/13 职场文书
村委会换届选举方案
2014/05/03 职场文书
《我爱祖国》演讲稿1000字
2014/09/26 职场文书
办公室行政主管岗位职责
2015/04/09 职场文书
MySQL详细讲解变量variables的用法
2022/06/21 MySQL