上二篇里,我們已經把留言本的功能、數據庫、文件、CSS等都設計好 了,現在我們要來完成代碼部份。 一、包含函數庫 <!--#include file="operation$db.asp" -->
在文件第一行加上包含語句 二、根據action 參數來判斷 <% action=request("action") select case action case "logout" logout '退出登錄子程序 case "login" login '登錄子程序 case "replay" replay '回貼子程序 case "delete" '表示刪除貼子 if session("lybmanage")="" then '如果還沒有登錄, response.redirect "index.asp?action=manage" '就顯示登錄界面 else delete '否則,去刪除貼子 end if case "addrec" '增加新貼子 addrec() '加入新貼 response.redirect "index.asp" '然后轉到初始頁 case "modify" '表示修改留言本信息 if session("lybmanage")="" then '如果還沒有登錄, response.redirect "index.asp?action=manage" '就顯示登錄界面 else modify '否則,修改信息 response.redirect "index.asp" '然后,回初始頁面 end if end select %>
三、根據上面的action,要有以下幾個子程序 <% '退出登錄子程序 sub logout() '刪除session變量lybmanage session.contents.remove "lybmanage" '轉到初始頁面 response.redirect "index.asp" end sub
'登錄子程序 sub login() '取得登錄參數 user=request.form("user") pwd=request.form("pwd") if user<>"" and pwd<>"" then '參數都不為空時才檢查 opendb my '打開數據庫連接 '定義SQL查詢,尋找用戶名相等的記錄 sql="select * from admin where admin_user='"&user&"'" '查詢表用于讀 searchtable my,sql,rs if not rs.eof then '如果有這樣的用戶 if pwd=rs("admin_pass") then '判斷密碼是否相等 session("lybmanage")=user '相等,就記錄到session變量里 end if end if end if response.redirect "index.asp" '不管登錄與否,都回到初始頁面 end sub
'修改留言本信息子程序 sub modify() '取得密碼 pwd=request.form("pwd") if pwd="" then exit sub '沒有密碼不修改 newpwd=request.form("newpwd")'取得新密碼 opendb my '打開數據庫連接 '定義SQL查詢語句 sql="select * from admin where admin_user='"&session("lybmanage")&"'" '打開表用于讀 changetable my,sql,rs if not rs.eof then '下面修改信息 if pwd=rs("admin_pass") then if newpwd<>"" then rs("admin_pass")=newpwd end if rs("admin_nick")=request.form("nick") rs("admin_name")=request.form("name") rs("admin_homepage")=request.form("homepage") rs("admin_perpage")=request.form("perpage") rs.update end if end if '關閉表和數據庫連接 closetable rs closedb my '完成后轉到初始頁面 response.redirect "index.asp" end sub
'回復子程序 sub replay() '如果沒有登錄,直接退出 if session("lybmanage")="" then exit sub '取得表單內容 rep=pro(request.form("rep")) id=request.form("id") '打開數據庫連接 opendb my '定義SQL查詢 sql="select * from main where user_id =" & id '打開表用于寫 changetable my,sql,rs '寫入 rs("user_replay")=true rs("user_rep")=rep rs("user_reptime")=now() rs.update '關閉表和數據庫連接 closetable rs closedb my '轉到初始頁 response.redirect "index.asp" end sub
'刪除貼子程序 sub delete() '如果沒登錄,直接退出 if session("lybmanage")="" then exit sub '取得刪除的貼子的ID id=request("id") '打開數據庫連接 opendb my '定義SQL查詢 sql="select * from main where user_id =" & id '打開表用于寫 changetable my,sql,rs '刪除貼 rs.delete rs.update '關閉表和數據庫連接 closetable rs closedb my '轉到初始頁面 response.redirect "index.asp" end sub
'加新貼子程序 sub addrec() '打開數據庫連接 opendb my '定義SQL查詢 sql="select * from main where user_id is null" '打開 changetable my,sql,rs '加入新記錄 rs.addnew rs("user_ip")=request.servervariables("remote_addr") rs("user_name")=request.form("name") rs("user_email")=request.form("email") rs("user_oicq")=request.form("oicq") rs("user_from")=request.form("from") rs("user_http")=request.form("http") rs("user_face")=request.form("face") '留言的內容要處理一下再入庫 rs("user_ly")=pro(request.form("ly")) rs.update '關閉表和數據庫連接 closetable rs closedb my end sub %>
四、在<html>標簽之前,還要加入讀取留言本信息的程序段,用來顯示導航條 <% opendb my opentable my,"main",rs homepage=rs("user_homepage") name=rs("user_nick") user=rs("user_name") perpage=rs("user_perpage") closetable rs closedb my %>
五、現在來完成最重要的幾個部份,也就是在<html></html>之間的部份。 有這么幾個: 最開始是顯示一個簡單的導航條 <A HREF="#top"></A> <TABLE cellSpacing=0 cellPadding=1 width=650 align=center bgColor=#000000 border=0> <TBODY> <TR> <TD> <TABLE class=table002 cellSpacing=0 cellPadding=4 width=650 border=0> <TBODY> <TR> <TD class=jnfont5 vAlign=center align=left><B>>> </B><A href="<%=homepage%>" title=返回主頁>主頁</A> | <A href="index.asp?action=showmodify" title=編輯你的個人資料>修改資料</A> | <%if session("lybmanage")="" then%> <A href="index.asp?action=manage" title=回復或刪除留言>留言管理</A> <%else%> <a href="index.asp?action=logout" title=退出管理模式>退出管理</a> <%end if%> </TD> <TD class=jnfont5 vAlign=center align=right> <%=name&"留言薄"%> </TD> </TR></TBODY></TABLE></TD></TR></TBODY></TABLE> <br>
然后根據參數選擇后面要顯示的部份 <%action=request("action") select case action case "" showadd showrec case "showmodify" if session("lybmanage")="" then showlogin else showmodify end if case "manage" if session("lybmanage")="" then showlogin else showadd showrec end if end select%>
下面是這幾個子程序: showadd 顯示加新貼表單子程序 <%sub showadd()%> <DIV align=center> <FORM onsubmit="return Juge(this)" action="index.asp" method="post"> <TABLE cellSpacing=1 cellPadding=3 width=650 bgColor=#000000 border=0> <TBODY> <TR> <TD class=table001 vAlign=top width=300 height=170> <TABLE cellSpacing=2 cellPadding=2 width="95%" border=0> <TBODY> <TR> <TD width="9%" align="center"><IMG height=24 src=http://cfan.net.cn/info/"images/nc.gif" width=24></TD> <TD class=jnfont3 width="14%">姓名</TD> <TD width="77%"><INPUT class=input1 maxLength=20 name=name> <INPUT type=hidden value=<%%> name=user> </TD></TR> <TR> <TD width="9%" align="center"><IMG height=16 src=http://cfan.net.cn/info/"images/email.gif" width=16 border=0></TD> <TD class=jnfont3 width="14%">Email</TD> <TD width="77%"><INPUT class=input1 maxLength=100 name=email> </TD></TR> <TR> <TD width="9%" align="center"><IMG height=16 src=http://cfan.net.cn/info/"images/from.gif" width=16></TD> <TD class=jnfont3 width="14%">來自</TD> <TD width="77%"> <select size=1 name=from> <option value=北京>北京 <option value=廣東>廣東 <option value=上海>上海 <option value=新疆>新疆 <option value=遼寧>遼寧 <option value=廣西>廣西 <option value=海南>海南 <option value=湖南>湖南 <option value=甘肅>甘肅 <option value=河北>河北 <option value=湖北>湖北 <option value=江西>江西 <option value=江蘇>江蘇 <option value=西藏>西藏 <option value=山東>山東 <option value=浙江>浙江 <option value=安徽>安徽 <option value=福建 selected>福建 <option value=吉林>吉林 <option value=黑龍江>黑龍江 <option value=山西>山西 <option value=云南>云南 <option value=貴州>貴州 <option value=四川>四川 <option value=陜西>陜西 <option value=重慶>重慶 <option value=天津>天津 <option value=河南>河南 <option value=青海>青海 <option value=寧夏>寧夏 <option value=臺灣>臺灣 <option value=香港>香港 <option value=澳門>澳門 <option value=其它地區>其它地區</option> </select> </TD> </TR> <TR> <TD width="9%" align="center"><IMG height=16 src=http://cfan.net.cn/info/"images/homepage.gif" width=16 border=0></TD> <TD class=jnfont3 width="14%">主頁</TD> <TD width="77%"> <INPUT class=input1 maxLength=100 size=30 name=http> </TD></TR> <TR> <TD width="9%" align="center"><IMG height=16 src=http://cfan.net.cn/info/"images/oicq.gif" width=16 border=0></TD> <TD class=jnfont3 width="14%">oicq</TD> <TD width="77%"><INPUT class=input1 maxLength=20 name=oicq> </TD></TR> <TR> <TD width="9%" align="center"><IMG height=18 src=http://cfan.net.cn/info/"images/icq.gif" width=18 border=0></TD> <TD class=jnfont3 width="14%">icq</TD> <TD width="77%"><INPUT class=input1 maxLength=20 name=icq> </TD></TR> <TR> <TD class=jnfont3 colSpan=2 align="center">您的尊容:</TD> <TD class=jnfont5 width="77%"><SELECT size=1 name=face> <OPTION value=images/1.gif selected >頭像1 <OPTION value=images/2.gif>頭像2 <OPTION value=images/3.gif>頭像3 <OPTION value=images/4.gif>頭像4 <OPTION value=images/5.gif>頭像5 <OPTION value=images/6.gif>頭像6 <OPTION value=images/7.gif>頭像7 <OPTION value=images/8.gif>頭像8 <OPTION value=images/9.gif>頭像9 <OPTION value=images/10.gif>頭像10 <OPTION value=images/11.gif>頭像11 <OPTION value=images/12.gif>頭像12 <OPTION value=images/13.gif>頭像13 <OPTION value=images/14.gif>頭像14 <OPTION value=images/15.gif>頭像15 <OPTION value=images/16.gif>頭像16 <OPTION value=images/17.gif>頭像17 <OPTION value=images/18.gif>頭像18 <OPTION value=images/19.gif>頭像19 <OPTION value=images/20.gif >頭像20 <OPTION value=images/21.gif >頭像21 <OPTION value=images/22.gif>頭像22 </OPTION></SELECT> <A href="javascript:popwin3('tou.htm')">查看頭像列表</A> </TD></TR></TBODY></TABLE></TD> <TD class=table001 vAlign=top width=300 height=170> <TABLE cellSpacing=2 cellPadding=2 width=292 align=center border=0> <TBODY> <TR> <TD width=10><IMG height=24 src=http://cfan.net.cn/info/"images/post.gif" width=24></TD> <TD class=jnfont3 width=119>內容</TD> <TD class=jnfont5 width=119> <DIV align=center><A href="javascript:popwin3('pubb.htm')">UBB代碼用法</A></DIV></TD></TR> <TR> <TD colSpan=3><TEXTAREA class=testarea1 name=ly rows=5 cols=44 type="text"></TEXTAREA> </TD></TR> <TR> <TD class=jnfont3 colSpan=3> 1)留言者不能發布任何不符合當地法規、國家法律和國際法律的資料; </TD></TR> <TR> <TD class=jnfont3 colSpan=3> 2)留言者不得發布任何非法的、騷擾性的、中傷他人的、辱罵性的、恐嚇性的、傷害性的、庸俗的,淫穢等信息資料; <input type="hidden" name="action" value="addrec"> <script language=JavaScript> <!--
function Juge(theForm) { if (theForm.name.value == "") { alert("請輸入您的姓名!"); theForm.name.focus(); return (false); } if (checktext(theForm.name.value)) { alert("請您輸入有效姓名!"); theForm.name.select(); theForm.name.focus(); return (false); } if (theForm.email.value == "") { alert("請您輸入\"email\"!"); theForm.email.focus(); return (false); } if (theForm.ly.value == "") { alert("請您輸入內容!"); theForm.ly.focus(); return (false); }
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@."; var checkStr = theForm.email.value; var allValid = true; for (i = 0; i < checkStr.length; i++) { ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break; } }
if (theForm.email.value.length < 6) { allValid = false; }
if (!allValid) { alert("您輸入的 \"電子郵件地址\" 無效!"); theForm.email.focus(); return (false); }
address=theForm.email.value; if(address.length>0) { i=address.indexOf("@"); if(i==-1) { window.alert("對不起!您輸入的電子郵件地址是錯誤的!") theForm.email.focus(); return false } ii=address.indexOf(".") if(ii==-1) { window.alert("對不起!您輸入的電子郵件地址是錯誤的!") theForm.email.focus(); return false }
} if (checktext(theForm.email.value)) { alert("請您輸入有效的\"email\"!"); theForm.email.select(); theForm.email.focus(); return (false); } }
function checktext(text) { allValid = true;
for (i = 0; i < text.length; i++) { if (text.charAt(i) != " ") { allValid = false; break; } }
return allValid; }
//--> </script> </TD> </TR></TBODY></TABLE></TD></TR> <TR class=table003> <TD colSpan=2> <DIV align=center> <INPUT class=input2 type=submit value=提交> <INPUT class=input2 type=reset value=重填> </DIV></TD></TR></TBODY></TABLE> </FORM> <%end sub%> </DIV>
showrec 顯示記錄子程序 <%sub showrec() page=1 if request("page")<>"" then page=cint(request("page")) end if
opendb my sql="select * from main order by user_time desc" searchtable my,sql,rs if rs.eof then closetable rs closedb my response.write "<center>還沒有人留言呢!</center>" exit sub end if rs.pagesize=perpage rs.absolutepage=page totalpage=rs.pagecount %> <DIV align=center> <table class=jnfont5 cellspacing=1 cellpadding=3 width=650 bgcolor=#000000> <tbody> <tr class=table003> <form action=index.asp method=get> <td align=middle width="40%"> <div align=center>關鍵字: <input type=hidden value=search name=action22> <input class=input2 size=15 name=key2> <input class=input2 type=submit value="搜索" name="submit2"> </div> </td> </form> <form name="change1" action=index.asp method=get> <td valign=center align="center"> <%if page=1 then response.write ("【首頁】【上頁】 ") else response.write ("【<a href=index.asp?page=1"&str&">首頁</a>】") response.write ("【<a href=index.asp?page="& page-1 & str & ">上頁</a>】") end if%> 轉到第 <select name="page" onChange="change1.submit()"> <%for i=1 to totalpage if page=i then response.write "<option selected>"&i&"</option>" else response.write "<option >" &i&"</option>" end if next %> </select> 頁 <% if page=totalpage then response.write ("【下頁】【尾頁】") else response.write ("【<a href=index.asp?page=" & page+1 & str & ">下頁</a>】") response.write ("【<a href=index.asp?page=" & totalpage & str& ">尾頁</a>】") end if%> </td> </form> </tr> </tbody> <br> <% for i=1 to perpage if not rs.eof then %> <TABLE cellSpacing=1 cellPadding=3 width=650 bgColor=#000000> <TBODY> <TR bgColor=#ffffff> <TD class=table001 vAlign=top width=120 rowSpan=2> <CENTER> <TABLE class=jnfont7> <FONT color=#ffffff><%=rs("user_name")%></FONT> <TBODY></TBODY> </TABLE> <IMG src="<%=rs("user_face")%>" border=0 width="82" height="90"> <BR> 來自- <%=rs("user_from")%> </CENTER> </TD> <TD class=table002 style="WORD-BREAK: break-all" vAlign=top width=530 height=105> <TABLE cellSpacing=0 cellPadding=0 width="100%" border="0"> <TBODY> <TR> <TD width=26><IMG height=24 src=http://cfan.net.cn/info/"images/post.gif" width=24></TD> <TD>留言內容: <%if rs("user_email")<>"" then%> <A title="給<%=rs("user_name")%>發信" href="mailto:<%=rs("user_email")%>"> <IMG height=16 src=http://cfan.net.cn/info/"images/email.gif" width=16 border=0>信箱</A> <%end if%> <%if rs("user_http")<>"" then%> <A title="訪問<%=rs("user_name")%>的主頁" href=<%=rs("user_http")%> target=_blank > <img height=16 src=http://cfan.net.cn/info/"images/homepage.gif" width=16 border=0>主頁</a> <%end if%> <%if rs("user_oicq")<>"" then%> <A title="<%=rs("user_name")%>的QQ號碼: <%=rs("user_oicq")%>" href="http://search.tencent.com/cgi-bin/friend/user_show_info?ln=<%=rs("user_oicq")%>" target=_blank> <IMG height=16 src=http://cfan.net.cn/info/"images/oicq.gif" width=16 border=0><%=rs("user_oicq")%></A> <%end if%> <IMG height=15 alt="<%=rs("user_ip")%>" src=http://cfan.net.cn/info/"images/ip.gif" width=16> <%=rs("user_ip")%> </TD> <TD width=80 align="center">第 <%=i+(page-1)*perpage%> 條 <a href="#top" title=回頂部>∧</a></TD> </TR> </TBODY> </TABLE> <HR SIZE=1> <%=rs("user_ly")%> </TD> </TR> <TR class=table001> <TD height=13 align="right"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <%if session("lybmanage")<>"" then response.write "<a href='index.asp?action=delete&id="&rs("user_id")&"'>刪除</a>" end if%> </td> <td align="right">發表于: <%=rs("user_time")%> <%if rs("user_replay") then%> 回復于:<%=rs("user_reptime")%> <%end if%> </td> </tr> </TD> </TR> </TBODY> <%if rs("user_replay") or session("lybmanage")<>"" then%> <tr class=table002> <td valign=top width=120> <div align=center>版主回復<br> <img src=http://cfan.net.cn/info/"images/repost.gif" border=0 width="24" height="24"><br> <table class=jnfont5> <%=user%>
</div> </td> <td width=530> <table class=jnfont5> <%if rs("user_replay") and session("lybmanage")="" then%><%=rs("user_rep")%> <%else%> <tbody><tr><td><form onsubmit="return check(this)" action="index.asp" method="post"> <textarea class=testarea1 name="rep" cols="44" rows="5"> <%if rs("user_replay") then response.write unpro(rs("user_rep"))%></textarea> <input type="hidden" name="action" value="replay"> <input type="hidden" name="id" value="<%=rs("user_id")%>"> <script language="JavaScript"> function check(theForm) { if (theForm.rep.value == "") { alert("請輸入回復內容!"); theForm.rep.focus(); return (false); } } </script> <input class=input2 type="submit" value="回復"> </form></td></tr></tbody> <%end if%> </td> </tr> <%end if%> </TABLE> <BR> <%rs.movenext end if next closetable rs closedb my end sub %>
顯示登錄表單子程序 <%sub showlogin()%> <br> <table cellspacing=1 cellpadding=0 width=200 align=center bgcolor=#000000 border=0> <tbody> <tr class=table003> <td width="50%"> <table class=jnfont7 align=center> <font color=#ffffff>管理員登錄</font> <tbody></tbody> </td> </tr> <tr class=table001> <td valign=top width="50%"> <table cellspacing=1 cellpadding=0 width="100%" border=0> <tbody> <form name="login" onsubmit="return check2(login)" action="index.asp" method="post"> <tr> <td class=jnfont3 width="100%"> <p align=center>用戶名: <input class=input1 maxlength=20 size=13 name="user"> <br> 密 碼: <input class=input1 type=password maxlength=20 size=13 name="pwd"> <br> <input type="hidden" name="action" value="login"> <input class=input1 type=submit value=" 登 陸 "> </p> </td> </tr> <tr> <td class=jnfont3 width="100%"> <p align=right> <script language="JavaScript"> function check2(theForm) { if (theForm.user.value == "") { alert("請輸入用戶名!"); theForm.user.focus(); return (false); } if (theForm.pwd.value == "") { alert("請輸入密碼!"); theForm.pwd.focus(); return (false); } } </script> 『<a href="index.asp">返回</a>』</p> </td> </tr> </form> </tbody> </tr> </tbody>
<%end sub%>
顯示修改信息表單 <%sub showmodify() opendb my opentable my,"admin",rs%> <table cellspacing=1 cellpadding=0 width=300 align=center bgcolor=#000000 border=0> <tbody> <tr class=table003> <td width="50%"> <table class=jnfont7 align=center> <font color=#ffffff>留言薄信息修改</font> <tbody></tbody> </td> </tr> <tr class=table001> <td valign=top width="50%"> <table cellspacing=1 cellpadding=0 width="100%" border=0> <tbody> <form onsubmit="return check(this)" action="index.asp" method="post"> <tr> <td class=jnfont3 width="30%" align="right" height="15">用戶名: </td> <td class=jnfont3 width="70%" height="15"><%=rs("admin_user")%> </td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">密 碼: </td> <td class=jnfont3 width="70%" height="15"> <input class=input1 type=password maxlength=20 size=13 name=pwd> 輸入密碼才能修改</td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">新密碼: </td> <td class=jnfont3 width="70%" height="15"> <input class=input1 type=password maxlength=20 size=13 name=newpwd> 不改請留空</td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">確定新密碼: </td> <td class=jnfont3 width="70%" height="15"> <input class=input1 type=password maxlength=20 size=13 name=newpwd2> 重輸新密碼</td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">昵稱: </td> <td class=jnfont3 width="70%" height="15"> <input class=input1 type=text maxlength=20 size=13 name=nick value="<%=rs("admin_nick")%>"> 留言薄名字</td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">姓名: </td> <td class=jnfont3 width="70%" height="15"> <input class=input1 type=text maxlength=20 size=13 name=name value="<%=rs("admin_name")%>"> 版主名字</td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">主頁: </td> <td class=jnfont3 width="70%" height="15"> <input class=input1 type=text maxlength=100 size=30 name=homepage value="<%=rs("admin_homepage")%>"> </td> </tr> <tr> <td class=jnfont3 width="30%" align="right" height="15">每頁顯示: </td> <td class=jnfont3 width="70%" height="15"> <select name="perpage"> <%for i=5 to 20 step 5 if rs("admin_perpage")=i then response.write "<option selected>"&i&"</option>" else response.write "<option >" &i&"</option>" end if next %> </select> 條留言 </td> </tr> <tr> <td class=jnfont3 width="100%" align="center" colspan="2" height="15"> <input type="hidden" name="action" value="modify"> <input class=input1 type=submit value=" 修 改 " name="submit"> </td> </tr> <tr> <td class=jnfont3 width="100%" colspan="2"> <p align=right> <script language="JavaScript"> function check(theForm) { if (theForm.pwd.value == "") { alert("請輸入密碼!"); theForm.pwd.focus(); return (false); } if (theForm.newpwd.value != "") { if (theForm.newpwd.value != theForm.newpwd2.value) { alert("兩次輸入的密碼不相同!"); theForm.newpwd.focus(); return (false); } } if (theForm.nick.value == "") { alert("請輸入昵稱!"); theForm.nick.focus(); return (false); } if (theForm.name.value == "") { alert("請輸入姓名!"); theForm.name.focus(); return (false); } if (theForm.homepage.value == "") { alert("請輸入您的主頁!"); theForm.homepage.focus(); return (false); } } </script> 『<a href="index.asp">返回</a>』</p> </td> </tr> </form> </tbody> </tr> </tbody>
<%closetable rs closedb my end sub %>
最后,加上文件尾部的版權引用: <!--#include file="bott.htm" -->
為了彈出一個窗口來顯示頭像和UBB用法,在<head></head>之間加入一個JS腳本: <script language=JavaScript> function popwin3(path){ window.open(path,"","height=450,width=600,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no"); } </script>
好了。所有的代碼都寫完了,最后就是測試了。 因為是第一次寫這么長的文章,不管是編排上還是內容上,總會有不足的地方,還請大家多多見諒 有什么意見,可以提出來,爭取以后寫得更好。 下載地址:http://www.he-xi.com/download/newlyb.zip 說明:下載后解壓到一個文件夾中,把這個文件傳到站點根目錄下,然后訪問該目錄下的index.asp 演示地址:http://www.he-xi.com/newlyb/
|