asp简单的ajax留言板(采用三层模式)
好久没有写过asp了,这回写个留言板还真觉得有点难度,竞然写了整整一天,哈哈.
就只有留言其它的都什么也没写,采用三层结构(不知道算不算,本来对三层的概念很糊涂)
演示的留言板,希望各位大哥大姐如果发现有漏洞的话请在这里告诉我,千万不要黑我的网站,在这里小弟先谢过了.
index.asp
<%@ codepage=65001%>
<%
option explicit
%>
<% response.charset="utf-8"%>
<% session.codepage=65001 %>
<!-- #include file="acchelper.asp" -->
<!-- #include file="common.asp"-->
<!-- #include file="dal_guest.asp" -->
<!-- #include file="mod_guest.asp" -->
<!-- #include file="bll_guest.asp" -->
<%
dim mybll
dim mylist
set mybll = new bll_guest
select case request("tcmd")
case "save"
mybll.insert()
case "del"
mybll.delete()
end select
mylist = mybll.findbypage()
%>
<h2>客户留言</h2>
<p>
<form name="subform" id="subform">
<textarea id="con" name="con" cols="56" rows="6" wrap="virtual"></textarea>
<input type="button" name="submit" value="提交留言" onclick="$('guest/index.asp?tcmd=save&content='+escape(this.form.con.value))" />
</form>
</p>
<%=mylist%>
bll_guest.asp
<%
'/// <summary>
'/// 摘要说明。
'/// </summary>
class bll_guest
private mycom,mymod,mydal
private li,ul
private del
'获取信息
public sub getguest()
mydal.getguest(id)
end sub
'新增信息
public sub insert()
mymod.content = request("content")
mymod.re = request("re")
mymod.addtime = now()
mymod.ip = request.servervariables("http_x_forwarded_for")
if len(mymod.ip)<=0 then mymod.ip = request.servervariables("remote_addr")
call mydal.insert(mymod)
end sub
'更新信息
public sub update()
call mydal.update(mymod)
end sub
'删除信息
public sub delete()
mydal.delete(request("id"))
end sub
'查找信息
public function findbypage()
dim pagesize,currentpage,wherevalue,ordervalue,recordcount
dim objrs
dim tmp1,tmp2,tmp3,parms,i
dim tcmd
tcmd = request("tcmd")
pagesize = "8"
currentpage = request("pageno")
if len(currentpage)<=0 then
currentpage=1
end if
wherevalue = ""
ordervalue = "id"
set objrs = mydal.findbypage(pagesize,currentpage-1,wherevalue,ordervalue)
i = 1
recordcount = mydal.count(wherevalue)
do while not objrs.eof
if tcmd="del" then
del = "<a href=""?tcmd=del&id="&objrs("id")&""">删除</a>"
else
del = ""
end if
parms=array(i,objrs("content"),objrs("ip"),objrs("addtime"),del)
tmp1 = tmp1 & mycom.format(li,parms)
objrs.movenext
i = i + 1
loop
tmp3 = mycom.page(pagesize,currentpage,recordcount,"guest/index.asp?tcmd=list")
parms=array(tmp1,tmp3)
tmp2 = tmp2 & mycom.format(ul,parms)
findbypage = tmp2
end function
private sub class_initialize()
li = "<li><span>{0}</span> <span>{1}</span><br /><span>作者:{2}</span> <span>{3}</span> <span>{4}</span></li>" & vbcrlf
ul = "<ul>{0}<li>{1}</li></ul>"
del = ""
set mycom = new common
set mymod = new mod_guest
set mydal = new dal_guest
end sub
private sub class_terminate()
set mycom = nothing
set mymod = nothing
set mydal = nothing
end sub
end class
%>
dal_guest.asp
<%
'/// <summary>
'/// 摘要说明。
'/// </summary>
class dal_guest
private sql_01
private sql_02
private sql_03
private sql_04
private sql_05
private sql_06
private sql_07
private mycom,myacc,mymod
'/// <summary>
'/// 获得信息
'/// </summary>
'/// <param name="id">判断字段</param>
'/// <returns>信息体</returns>
public function getguest(id)
dim parms,sql,objrs
parms = array(id)
sql = mycom.format(sql_01,parms)
set objrs = myacc.executereader(myacc.conn_string,sql)
if not objrs.eof then
call mymod.setvar(objrs("id"),objrs("content"),objrs("re"),objrs("addtime"),objrs("ip"))
set getguest = mymod
else
getguest = null
end if
end function
'/// <summary>
'/// 插入信息
'/// </summary>
'/// <param name="">信息体</param>
public sub insert(vmod)
dim parms,sql
parms = array(vmod.content,vmod.re,vmod.addtime,vmod.ip)
sql = mycom.format(sql_02,parms)
call myacc.executereader(myacc.conn_string,sql)
end sub
'/// <summary>
'/// 更新信息
'/// </summary>
'/// <param name="">信息体</param>
public sub update(vmod)
dim parms,sql
parms = array(vmod.id,vmod.content,vmod.re,vmod.addtime,vmod.ip)
sql = mycom.format(sql_03,parms)
call myacc.executereader(myacc.conn_string,sql)
end sub
'/// <summary>
'/// 删除信息
'/// </summary>
'/// <param name="">信息体</param>
public sub delete(id)
dim parms,sql
parms = array(id)
sql = mycom.format(sql_04,parms)
call myacc.executenonquery(myacc.conn_string,sql)
end sub
'/// <summary>
'/// 统计信息
'/// </summary>
'/// <param name="">信息体</param>
public function count(wherevalue)
dim parms,sql,objrs
parms = array(wherevalue)
sql = mycom.format(sql_07,parms)
set objrs = myacc.recordset(myacc.conn_string,sql)
count = objrs.recordcount
end function
'/// <summary>
'/// 根据条件列出所有信息(带有分页)
'/// </summary>
'/// <param name="pagesize">每页记录数</param>
'/// <param name="currentpage">页码</param>
'/// <param name="wherevalue">判断值</param>
'/// <param name="ordervalue">排序</param>
'/// <returns>返回objrs</returns>
public function findbypage(pagesize,currentpage,wherevalue,ordervalue)
dim parms,sql,objrs
dim s1
if currentpage="0" then
s1 = pagesize & "|" + ordervalue & "|" & wherevalue
parms = split(s1,"|")
sql = mycom.format(sql_05,parms)
else
s1 = pagesize & "|" & ordervalue & "|" + wherevalue & "|" & currentpage*pagesize
parms = split(s1,"|")
sql = mycom.format(sql_06,parms)
end if
set objrs = myacc.executereader(myacc.conn_string,sql)
set findbypage = objrs
end function
private sub class_initialize()
sql_01 = "select * from [guest] where id = {0}"
sql_02 = "insert into [guest] ([content],[re],[addtime],[ip]) values('{0}','{1}','{2}','{3}')"
sql_03 = "update [guest] set [content]='{1}',[re]='{2}',[addtime]='{3}',[ip]='{4}' where id = {0}"
sql_04 = "delete from [guest] where id in ({0})"
sql_05 = "select top {0} * from [guest] where content like '%{2}%' order by {1} desc,id desc"
sql_06 = "select top {0} * from [guest] where content like '%{2}%' and id not in(select top {3} id from [guest] where content like '%{2}%' order by {1} desc,id desc) order by {1} desc,id desc"
sql_07 = "select * from [guest] where content like '%{0}%' "
set mycom = new common
set myacc = new acchelper
set mymod = new mod_guest
end sub
private sub class_terminate()
set mycom = nothing
set myacc = nothing
set mymod = nothing
end sub
end class
%>
mod_guest.asp
<%
'/// <summary>
'/// 摘要说明。
'/// </summary>
class mod_guest
public id
public content
public re
public addtime
public ip
public sub setvar(vid,vcontent,vre,vaddtime,vip)
id=vid
content=vcontent
re=vcontent
addtime=vaddtime
ip=vip
end sub
end class
%>
common.asp
<%
'/// <summary>
'/// 摘要说明
'/// </summary>
class common
'/// <summary>
'/// 获得信息
'/// </summary>
'/// <param name="id">需要内容</param>
'/// <param name="id">替换关键字</param>
'/// <returns>格式化后内容</returns>
public function format(str,arr)
dim r,i
r = str
for i = 0 to ubound(arr)
r = replace(r,"{"&i&"}",arr(i))
next
format = r
end function
public function page(pagesize,currentpage,recordcount,pageurl)
const c_recordcount = "合计<strong><font color=""red"">{0}</font></strong>篇 |"
const c_firstpage1 = " <a href=""#"" onclick=""$('{0}&pageno={1}')"">首页</a>"
const c_firstpage2 = " 首页"
const c_prevpage1 = " <a href=""#"" onclick=""$('{0}&pageno={1}')"">上一页</a>"
const c_prevpage2 = " 上一页"
const c_nextpage1 = " <a href=""#"" onclick=""$('{0}&pageno={1}')"">下一页</a>"
const c_nextpage2 = " 下一页"
const c_lastpage1 = " <a href=""#"" onclick=""$('{0}&pageno={1}')"">尾页</a>"
const c_lastpage2 = " 尾页"
const c_currentpage = " 页次:<strong><font color=""red"">{0}</font>/{1}</strong>页"
const c_pagesize = " <strong>{0}</strong>篇/页 "
const c_gotopage = " 跳转:<input type=""text"" name=""goto"" value=""{1}"" onchange=""$('{0}&pageno='+this.value)"" style=""width:20px;border:1px""/> "
dim pagecount,pages
dim lrecordcount,lfirstpage,lprevpage,lnextpage,llastpage,lcurrentpage,lpagesize,lgotopage
dim p,parms
if recordcount mod pagesize <> 0 then
pages = 1
else
pages = 0
end if
pagecount=cint(recordcount/pagesize + pages)
parms=array(recordcount)
lrecordcount = format(c_recordcount,parms)
if cint(currentpage)>1 then
parms=array(pageurl,1)
lfirstpage = format(c_firstpage1,parms)
parms=array(pageurl,cint(currentpage)-1)
lprevpage = format(c_prevpage1,parms)
else
lfirstpage=c_firstpage2
lprevpage=c_prevpage2
end if
if cint(currentpage)<pagecount then
parms=array(pageurl,cint(currentpage)+1)
lnextpage = format(c_nextpage1,parms)
parms=array(pageurl,cint(currentpage)+1)
llastpage = format(c_lastpage1,parms)
else
lnextpage=c_nextpage2
llastpage=c_lastpage2
end if
parms=array(currentpage,pagecount)
lcurrentpage = format(c_currentpage,parms)
parms=array(pagesize)
lpagesize = format(c_pagesize,parms)
parms=array(pageurl,currentpage)
lgotopage = format(c_gotopage,parms)
p=lrecordcount&lfirstpage&lprevpage&lnextpage&llastpage&lcurrentpage&lpagesize&lgotopage
page = p
end function
end class
%>
acchelper.asp
<%
'/// <summary>
'/// acchelper 的摘要说明。
'/// </summary>
class acchelper
public conn_string
'/// <summary>
'/// 执行executenonquery
'/// </summary>
'/// <param name="connstring">连接字符</param>
'/// <param name="cmdtext">sql命令</param>
public function executenonquery(connstring,cmdtext)
dim objconn
set objconn = server.createobject("adodb.connection")
objconn.open connstring
objconn.execute(cmdtext)
end function
'/// <summary>
'/// 执行executereader
'/// </summary>
'/// <param name="connstring">连接字符</param>
'/// <param name="cmdtext">sql命令</param>
'/// <returns>rs</returns>
public function executereader(connstring,cmdtext)
dim objconn,objrs
set objconn = server.createobject("adodb.connection")
objconn.open connstring
set objrs = objconn.execute(cmdtext)
set executereader = objrs
end function
'/// <summary>
'/// 执行recordset
'/// </summary>
'/// <param name="connstring">连接字符</param>
'/// <param name="cmdtext">sql命令</param>
'/// <returns>rs</returns>
public function recordset(connstring,cmdtext)
dim objconn,objrs
set objconn = server.createobject("adodb.connection")
objconn.open connstring
set objrs = server.createobject("adodb.recordset")
objrs.open cmdtext,objconn,1,3
set recordset = objrs
end function
private sub class_initialize()
conn_string = "provider = microsoft.jet.oledb.4.0;data source=" & server.mappath("db.mdb")
end sub
end class
%>
ajax.js
var http_request = false;
function makerequest(obj,url) {
http_request = false;
if (window.xmlhttprequest) { // mozilla, safari,...
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('giving up :( cannot create an xmlhttp instance');
return false;
}
http_request.onreadystatechange = function()
{
if (http_request.readystate == 4) {
if (http_request.status == 200) {
updateobj(obj,http_request.responsetext);
}
else
{
updateobj(obj,"");
}
}
}
http_request.open('get', url, true);
http_request.send(null);
}
function updateobj(obj,data){
obj.innerhtml = data;
}
function $(url){
var obj = document.getelementbyid("content");
obj.style.display='block';
document.getelementbyid("container").style.margin="10px auto 0px";
obj.innerhtml = "loading…";
makerequest(obj,url);
}
html调用方法:
<a href="#" onclick="$('news.html')">新闻</a><a href="#" onclick="$('product.html')">产品</a><a href="#" onclick="$('case.html')">案例</a><a href="#" onclick="$('contact.html')">联系</a><a href="http://zj55com.cnblogs.com/" target="_blank">博客</a>