凹丫丫网络社区's Archiver

Amm 发表于 2008-5-17 16:03

用ASP设计一个留言薄(下)

<DIV class=NewsContent id=NewsContentLabel>现在说说book.htm,让大家了解一下其中的那个输入FORM的作用,它的action对象是本身,book.asp(等一会儿需要把book.htm改名为book.asp)这个文件,也就是说。表单提交后,由本身来作处理。在这个FORM中,有几个文本输入框,它们分别是name,sex,from,url,email,以及多行文本输入框comments。 <BR><BR>  现在把book.htm改名为book.asp,往文件中添加ASP代码。如下:(单引号表示ASP的注析,编译器是不处理的。 <BR><BR>  *book.asp <BR><BR><% @language="vbscript" %><BR><%<BR>‘'判断请求方式,当客户端请求文件时,比如直接输入网址:http://active.silversand.net/index.htm这样,就会产生一个GET的请求信息。而提交表单时(表单的方式设为POST),就会产生POST的请求方式,现在判断book.asp这个文件是用户用浏览器打开页面,还是用户提交数据这个动作,以便进行程序处理。 <BR>if Request.ServerVariables("REQUEST_METHOD")="POST" then<BR>‘这里判断出是由用户提交表单而调入入的页面,因此进行登记留言处理。 <BR>‘创建一个文件组件的服务器实例<BR><BR>set fsFilesys=CreateObject("Scripting.FileSystemObject")<BR>‘打开文本文件server.mappath("/")是返回服务器发行目录的真实路径,也就是比如说:c:\Inetpub\wwwroot这个路径,然后"/realchat/book/book.txt"就是相对于这个路径基础上的路径。<BR><BR>set myText=fsFilesys.OpenTextFile(Server.MapPath("/")&amp;"/realchat/book/book.txt")<BR>‘把文本文件的所有内容读出,保存在Temp这个变量中。<BR><BR>Temp=myText.Readall<BR>myText.Close<BR>‘是否需要添加内容<BR><BR>if Request("task")="insert" Then<BR>‘取得表单的各项的值<BR><BR>Tname=request.form("name")<BR>Tsex=request.form("sex")<BR>Temail=request.form("email")<BR>turl=request.form("url")<BR>Tfrom=request.form("from")<BR>Tcomments=request.form("comments")<BR>Ttime="#&amp;@"&amp;time<BR>if Len(Tcomments)<2 Then<BR>%><BR><font size="4" color="red"><BR><BR><p align="center">对不起!你还未留言。<a href="book.asp">返回</a></font> <%<BR>else<BR>if Lcase(Left(Turl,7))<>"http://" Then<BR>Turl="http://"&amp;Turl<BR>End if<BR>Turl=Lcase(Turl)<BR>‘重新创建数据文件,把原来的覆盖掉。<BR><BR>set myText=fsFilesys.CreateTextFile(Server.MapPath("/")&amp;"/realchat/book/book.txt")<BR>myText.WriteLine(Tname)<BR>myText.WriteLine(Tsex)<BR>myText.WriteLine(Temail)<BR>myText.WriteLine(Turl)<BR>myText.WriteLine(Tfrom)<BR>myText.WriteLine(Tcomments)<BR>myText.writeLine(Ttime)<BR>myText.WriteLine(date)<BR>myText.writeLine(Temp)<BR>myText.Close<BR>%><BR><script Language="JavaScript"><BR>document.write("<h1>恭喜!您的留言已顺利提交,请按<a href='disp.asp'>这里</a>查看")<BR></script><BR><%<BR><BR>end if<BR>‘至此,留言已保存入文件book.txt中<BR><BR>End if<BR><BR>else<BR>‘第一次进入输入屏幕,因此显示htmL文件内容。<BR>%><BR><html><BR><BR><head><BR><title>留言薄</title><BR><% colorbg="#BED9FC" %><BR></head><BR><BR><body bgcolor="<% =colorbg %>" background="images/bg.gif"><BR><BR><BR><p align="center"><img src=http://www.yule21.com/info/"images/gbook.gif" alt="GBook.gif (12142 bytes)" WIDTH="374"<BR>HEIGHT="83"> </p><BR><BR><p align="center">[<a href="disp.asp">我要看留言板</a>]&nbsp; [<a href="../index.asp">返回首页</a>]</p><BR><BR><form method="POST" action="book.asp"><BR><input type="hidden" name="task" value="insert"><div align="left"><table><BR><tr><BR><td align="right">姓名</td><BR><td><img src=http://www.yule21.com/info/"images/bottom-b.gif" alt="BUTTOM-B.GIF (179 bytes)" WIDTH="16" HEIGHT="16"></td><BR><td><input name="name" size="46"></td><BR></tr><BR><tr><BR><td align="right">性别</td><BR><td><img src=http://www.yule21.com/info/"images/bottom-g.gif" alt="BUTTOM-G.GIF (179 bytes)" WIDTH="16" HEIGHT="16"></td><BR><td><input name="sex" size="46"></td><BR></tr><BR><tr><BR><td align="right">来自</td><BR><td><img src=http://www.yule21.com/info/"images/bottom-b.gif" alt="BUTTOM-B.GIF (179 bytes)" WIDTH="16" HEIGHT="16"></td><BR><td><input name="from" size="46"></td><BR></tr><BR><tr><BR><td align="right">主页URL</td><BR><td><img src=http://www.yule21.com/info/"images/homepage.gif" alt="HomePage.gif (1232 bytes)" WIDTH="30" HEIGHT="30"></td><BR><td><input name="url" size="46"></td><BR></tr><BR><tr><BR><td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>E-mail</td><BR><td><img src=http://www.yule21.com/info/"images/email.gif" alt="Email.gif (1197 bytes)" WIDTH="30" HEIGHT="30"></td><BR><td><input name="email" size="46"></td><BR></tr><BR><tr><BR><td align="right">留言</td><BR><td colspan="2"><textarea wrap="off" name="comments" cols="49" rows="9"></textarea></td><BR></tr><BR><tr><BR><td>&nbsp;&nbsp;&nbsp;&nbsp; </td><BR><td colspan="2"><dl><BR><dd align="center">&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" value="我写好了"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="reset" value="重新再写"></dd><BR></dl><BR></td><BR></tr><BR></table><BR></div><BR></form><BR><BR><p align="center"> </p><BR></body><BR></html><BR><%<BR>end if<BR>%> <BR><BR>  现在到disp.asp这个文件,这个文件主要完成从book.txt中提取文件内容,然后重新生成htmL代码以显示出留言内容。 <BR><BR>  代码如下: <BR><BR><% @language="vbscript" %><BR><BR><html><BR><BR><head><BR><title>留言薄</title><BR></head><BR><BR><body background="images/back.gif"><BR><BR><p align="center"><img src=http://www.yule21.com/info/"images/gbook.gif" alt="gbook.gif (12142 bytes)" WIDTH="374" HEIGHT="83"><%<BR>‘打开数据文件<BR><BR>set fsFilesys=CreateObject("Scripting.FileSystemObject")<BR>set myText=fsFilesys.OpenTextFile(Server.MapPath("/")&amp;"/realchat/book/book.txt")<BR>Tname=myText.ReadLine<BR>%> </p><BR><BR><p align="center">[<font color="#00FFFF" size="4"><a href="book.asp">我要写留言</a></font>][<font color="#00FFFF"><a href="../index.asp">返回首页</a></font>]</p><BR><BR><table width="600"><BR><%<BR>do while Tname<>"Line1"<BR>‘逐行读出内容(Line1)是文件结束标志<BR><BR>Tsex=myText.ReadLine<BR>Temail=myText.ReadLine<BR>Turl=myText.ReadLine<BR>Tfrom=myText.ReadLine<BR>%><BR><tr><BR><td><hr><BR></td><BR></tr><BR><tr><BR>‘<%=表达式%>是向客户端输出结果<BR><BR><th colspan="2" align="left">姓&nbsp;&nbsp;&nbsp;名:<% =Tname %><br><BR>性&nbsp;&nbsp;&nbsp;别:<% =Tsex %><br><BR>E—mail:<a href="mailto:<% =Temail %>"><% =Temail %></a><br><BR>主页URL:<a href="<% =Turl %>" target="_blank"><% =turl %></a><br><BR>来&nbsp;&nbsp;&nbsp;自:<% =Tfrom %></th><BR></tr><BR><tr><BR><th align="left" bgcolor="<% =colorbg %>"><font color="red">留&nbsp;&nbsp;&nbsp;言:</font></th><BR><th align="left" bgcolor="<% =colorbg %>"><%<BR>Tcomments=myText.ReadLine<BR>do while InStr(Tcomments,"#&amp;@")=0<BR>%><BR><% =Tcomments %><BR><br><BR><%<BR>Tcomments=myText.ReadLine<BR>loop<BR>Ttime=Mid(Tcomments,4)<BR>Tdate=myText.ReadLine<BR>%> </th><BR></tr><BR><tr><BR><th colspan="2" align="left">时&nbsp;&nbsp;&nbsp;间:<% =Ttime %> <br><BR>日&nbsp;&nbsp;&nbsp;期:<% =Tdate %></th><BR></tr><BR><tr><BR><td><br><BR><%<BR>tname=myText.ReadLine<BR>loop<BR>myText.Close<BR>%> </td><BR></tr><BR></table><BR><BR><p><br><BR><br><BR></p><BR></body><BR></html> <BR><BR>  好了,仔细地把源文件DOWN下去,自己不断修改,不断测试一下,这样对学习很有好处,看看book.txt的内容。 <BR><BR>  *book.txt的内容,里面有一条留言 <BR><BR>  小Hu<BR>  男<BR>  realplay@163.net<BR>  http://realplay.163.net<BR>  佛山<BR>  热烈兴祝“泰信心动聊天室虚拟社区”进入测试使用阶断,衷心感谢些您的到来,在测试过程中有什么问题或好的建议,请告诉我。<BR>  yesky诚邀您的加盟参与!<BR>  webmaster<BR><BR>  wak@21:51:15<BR>  99-7-13<BR>  Line1<BR><BR>  这个留言本很简单,但我觉得很有亲切感,比那些比较“现代化”的比如说心动留言薄这类的风格,朴素得多。 <BR></DIV>

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.