ASP留言板的设计与实现
1、开始时建立数据库,我们新建一个数据库,进入企业管理器,右击数据库,选择新建数据库
2、输入一个数据库名称
3、选中新好的数据库,在右边的窗口中,右击,弹出的菜单里选新建数据表
4、表名:message
ID int 4
memo text 16
time1 nvarchar 50
hui text 16
id自动加1
memo是留言内容
time1留言时间
hui回复内容
添加,4个字段
然后设置ID字段为自动加1,如下图所示
5、新建一个站点,命名为message
新建一个文件夹在D盘,命名为留言板,把IIS的物理路径(参考aspIIS设置的那篇文章
http://jingyan.baidu.com/article/fedf0737663b3935ac8977fa.html
)设置为D:\留言板
把站点的文件路径也设为:D:\留言板
6、在Adobe Dreamweaver CS4 中新建文件
7、点击创建一个html的文档。
8、然后修改这个文档的编码,把编码改为GB2312。
9
10、然后在文件中删除默认的那些代码,重新输入以下新代码
下面的代码是接sql2000数据库
<%
Dim Database
Database="datamessage"
Dim Conn,ConnStr
Set conn = Server.CreateObject("ADODB.Connection")
ConnStr="Provider=sqloledb;Server=localhost;database="&Database&";uid=sa;pwd="
Conn.open ConnStr
%>
然后保存为conn.asp文件
注意选保存文件的类型为所有文件(*.*)
11、再次新建一个名为:default.asp的文件,这个文件是显示和添加留言的页面
编码也需要改为GB2312
在这里输入的代码如下:
<!--#include file="conn.asp"-->
<%
if request("action")="addmessagesave" then
if trim(request("memo"))="" then
Response.Write("<script lanuage=javascript>alert('留言内容不能为空!');location='message.asp'</script>")
response.End()
End if
set rsa=Server.CreateObject("ADODB.RecordSet")
strsqla="select * from [message]"
rsa.open strsqla,conn,3,2
rsa.addnew
rsa("memo")=request("memo")
rsa("time1")=now
rsa.update
Response.Write("<script lanuage=javascript>alert('留言成功!');location='message.asp'</script>")
response.End()
End if
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>在线留言</title>
<style>
*{margin:0px;padding:0px;font-size:14px;color:#000000;}
a{font-size:14px;color:#999999;}
body{margin:0 auto;line-height:30px;}
</style>
</head>
<body>
<table width="930" height="553" border="0" align="center" cellpadding="0" cellspacing="0" >
<tr>
<td width="230" valign="top" align="center">
<br/><br/><br/>
<a href="manage.asp">留言管理</a>
<br/><br/>
</td>
<td width="700" height="553" valign="top" align=left>
留言 <div class=height9> </div>
<br/><br/>
<br/>
<%
message
addmessage
%>
</td>
</tr>
</table>
</body>
</html>
<%
sub addmessage()
%>
<form id="form1" name="form1" method="post" action="?action=addmessagesave">
<table width="519" height="200" border="0">
<tr>
<td width="94" height="55"> </td>
<td width="415">
<input type="hidden" name="dd" value="0" checked="checked"/> </td>
</tr>
<tr>
<td height="104" align=center>内容:</td>
<td><label>
<textarea name="memo" cols="50" rows="6" ></textarea>
</label></td>
</tr>
<tr>
<td height="33"> </td>
<td>
<input type="submit" name="Submit" value="提交" /> </td>
</tr>
</table>
</form>
<%
end sub
SUB message()
set rs=Server.CreateObject("ADODB.RecordSet")
sql="select * from message order by id desc"
rs.open sql,conn,1,1
if rs.eof then
if request("key")<>"" then
response.write("<div style=""text-align:center"">没有查到您要查找的留言!</div>")
else
response.write("<div style=width:500px;height:50px;text-align:center>暂时没有留言留言!</div>")
End if
else
response.write("<div >")
PERPAGE=10
if request("pageno")="" or isNumeric(request("pageno"))=0 or request("pageno")<"1" then
curpage = 1
else
curpage = cint(request("pageno"))
end if
rs.pagesize = PERPAGE
if curpage>rs.pagecount then curpage=rs.pagecount
rs.absolutepage = curpage
for i=1 to rs.pagesize
if rs.eof then
exit for
end if
%>
<table width="553" height="100" border="0" cellpadding="0" cellspacing="0" bgcolor=#ffffff style="border:1px solid #cccccc;margin-left:10px;margin-top:5px;">
<tr>
<td style="padding-left:20px;">[内容]</td>
<td class="xb-a04"><%=rs("memo")%></td>
<td> </td>
</tr>
<tr>
<td height="31" style="padding-left:20px;"></td>
<td class="xb-a04"><%
%></td>
<td>[时间:<%=rs("time1")%>]</td>
</tr>
<%if rs("hui")<>"" then%>
<tr>
<td style="padding-left:20px;">[回复]</td>
<td colspan="2" class="xb-a04" align="left"> <%=rs("hui")%></td>
</tr>
<%End if%>
</table>
<%
rs.movenext
next
if curpage = 1 then
response.write "</div><BR><div style=width:440px;height:30px;text-align:center> 首页 | 上一页 | "
else
response.write "</div><BR><div style=width:440px;height:30px;text-align:center; ><a href='?pageno=1'>首页</a> | <a href='?pageno="&curpage-1&"&leixing="&leixing&"'>上一页</a> | "
end if
if curpage = rs.pagecount then
response.write "下一页 | 尾页</div>"
else
response.write "<a href='?pageno="&curpage+1&"&leixing="&leixing&"'>下一页</a> | <a href='?pageno="&rs.pagecount&"&leixing="&leixing&"'>尾页</a></div>"
end if
rs.close
end if
End sub
%>
12、新建一个管理留言的页面,名为manage.asp
同样先改编码为GB2312去掉原来的代码重新输入新的代码如下:
<!--#include file="conn.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<style type="text/css">
<!--
body {font-size: 12px; color: #000000; font-family: 宋体}
td {font-size: 12px; color: #000000; font-family: 宋体;}
.t1 {font:12px 宋体;color=000000}
.t2 {font:12px 宋体;color:ffffff}
.bt1 {font:14px 宋体;color=000000}
.bt2 {font:14px 宋体;color:ffffff}
A:link {color: #000066}
A:visited {color: #000066}
A:hover {color: #ff0000}
A.r1:link {font-size:12px;text-decoration:underline;color:#000000;}
A.r1:visited {font-size:12px;text-decoration:underline;color:#000000;}
A.r1:hover {font-size:12px;text-decoration:underline;color:#ff0000;}
A.r2:link {font-size:12px;text-decoration:underline;color:#ffffff;}
A.r2:visited {font-size:12px;text-decoration:underline;color:#ffffff;}
A.r2:hover {font-size:12px;text-decoration:underline;color:#ff6600;}
A.r3:link {font-size:12px;text-decoration:none;color:#000000;}
A.r3:visited {font-size:12px;text-decoration:none;color:#000000;}
A.r3:hover {font-size:12px;text-decoration:underline;color:#ff0000;}
.bd{ background:#03F}
-->
</style>
</head>
<body>
<%
dim filename
dim biaoname
filename="message.asp"
biaoname="message"
%>
<center>
<table width="784" height="45" border="0" align="center">
<tr>
<td width="386" height="41" align=right>留言-管理</td>
<td width="382"> </td>
</tr>
<tr>
<td width="386" height="41" align=right> </td>
<td width="382"> </td>
</tr>
</table>
<%
if request("action")="" then '显示信息
%>
<TABLE width="393" border="0" cellpadding="4" cellspacing="1" bgcolor="#ffffff" align="center">
<tr height=20 bgcolor="BEBFD9">
<TD width="46" height="20" align="center" class=bd><font color="#FFFFFF"><b>编号</b></font></TD>
<TD width="63" align="center" class=bd><font color="#FFFFFF"><b>留言时间</b></font></TD>
<TD width="98" align="center" class=bd><font color="#FFFFFF"><b>内容</b></font></TD>
<TD width="59" align="center" class=bd><font color="#FFFFFF"><b>回复</b></font></TD>
<TD width="59" align="center" class=bd><font color="#FFFFFF">
<input type='submit' value='删除'>
</font></TD>
</TR>
<%
dim strsql
set rs=server.createobject("adodb.recordset")
strsql="select * from "&biaoname&" order by Id desc"
rs.open strsql,Conn,3,2
if rs.eof then
response.write("<div style=width:500px;height:500px;text-align:center>没有信息</div>")
else
'response.write("<div style=width:500px;height:500px;>")
PERPAGE=15
if request("pageno")="" or isNumeric(request("pageno"))=0 or request("pageno")<"1" then
curpage = 1
else
curpage = cint(request("pageno"))
end if
rs.pagesize = PERPAGE
if curpage>rs.pagecount then curpage=rs.pagecount
rs.absolutepage = curpage
%>
<%
for i=1 to rs.pagesize
if rs.eof then
exit for
end if
%>
<tr height=20 <%if i mod 2 = 0 then
response.write("bgcolor=#cccccc")
else
response.write("bgcolor=#EEEeee")
End if
%>>
<TD height="27" align="center" ><font color="#000000"><b><%=rs("id")%></b></font></TD>
<TD align="center" ><font color="#000000"><b><%=rs("time1")%></b></font></TD>
<TD align="center" ><font color="#000000"><b><%=rs("memo")%></b></font></TD>
<TD align="center" ><a href=?action=hui&id=<%=rs("id")%>>点击回复</a></TD>
<TD align="center" ><a href=?action=del&id=<%=rs("id")%>>删除</a></TD>
</TR>
<%if rs("hui")<>"" then%>
<tr height=20 bgcolor="#eeeeee" >
<TD width="46" height="27" align="center" >回复:</TD>
<TD colspan="4" align="left" >
<%=rs("hui")%>
</TD>
</TR>
<%End if%>
<%
rs.movenext
next
%>
</TABLE>
<% if curpage = 1 then
response.write "<br/><div style=width:500px;height:30px;text-align:center> 首页 | 上一页 | "
else
response.write "<br/><div style=width:500px;height:30px;text-align:center><a href='?pageno=1'>首页</a> | <a href='?pageno="&curpage-1&"&typeid="&request("typeid")&"'>上一页</a> | "
end if
if curpage = rs.pagecount then
response.write "下一页 | 尾页</div>"
else
response.write "<a href='?pageno="&curpage+1&"&leixing="&leixing&"'>下一页</a> | <a href='?pageno="&rs.pagecount&"&typeid="&request("typeid")&"'>尾页</a></div>"
end if
rs.close
end if
end if
'--------------------------------------------------------------修改完成----------------
if request("action")="del" then '删除
id=request("id")
set rs=server.createobject("adodb.recordset")
sql="SELECT * from "&biaoname&" where id="&cstr(id)
rs.open sql,conn,1,3
rs.delete
'rs.update
response.Write "<script>alert('删除成功!');this.location.href='"&filename&"';</SCRIPT>"
end if
if request("action")="hui" then '回复
%>
<form action="?action=huisave" method="post">
<table width="454" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="52" height="168">回复:</td>
<td width="402"><label>
<textarea name="hui" id="textarea" cols="45" rows="5"></textarea>
</label>
<input type="hidden" name="id" value="<%=request("id")%>">
</td>
</tr>
<tr>
<td height="59"> </td>
<td><label>
<input type="submit" name="button" id="button" value="提交">
</label></td>
</tr>
</table>
</form>
<%End if
if request("action")="huisave" then '删除
if request("hui")="" then
response.write ("<script>window.alert('回复内容不能为空!');this.location.href='message.asp'</script>")
response.end
End if
set rs1=server.CreateObject("ADODB.RecordSet")
sql="SELECT * from "&biaoname&" where id="&request("id")
rs1.open sql,conn,3,2
rs1("hui")=request("hui")
rs1.update
rs1.close
response.write ("<script>window.alert('回复成功!');this.location.href='message.asp'</script>")
End if
%>
</center>
</body>
</html>
13、运行调式程序
打开一个IE浏览器
输入:http://localhost/
地址
就可以进入到留言板界面,进行留言了
点击左边的留言管理,就可以进入留言管理界面,在留言管理界面可以回复和删除留言信息。