隨著網(wǎng)絡的普及,網(wǎng)上調(diào)查往往能達到較好的效果。在自己的個人站點上放一個趣味問題調(diào)查板,著實能為網(wǎng)站添色不少;況且通過調(diào)查結(jié)果你能更多更準確了解網(wǎng)友對自己站點的看法。作為Webmaster,如果你的網(wǎng)站也需要就某個主題進行調(diào)查,而你又討厭使用別人免費提供的調(diào)查板(免費往往是要付出代價的,比如打廣告!),那么我建議你花十幾分鐘時間看完這篇介紹用ASP制作調(diào)查板的文章。你只要把文中的代碼Copy到你的機子上,稍做修改,你立即就擁有一個屬于你自己的個性化調(diào)查板。好了,打起精神往下看吧。 本調(diào)查板共有三個文件:顯示調(diào)查問題(research.html)、處理用戶選擇(select.asp)、瀏覽調(diào)查結(jié)果(viewresult.asp)。設計思路為:ASP取得由表單發(fā)來的信息,并據(jù)此修改記錄調(diào)查得票數(shù)的數(shù)據(jù)庫,然后ASP讀取數(shù)據(jù)庫,獲得各個調(diào)查問題的得票數(shù)目,通過得票數(shù)多少來調(diào)節(jié)對應條形圖顯示的寬來直觀比例地給出調(diào)查結(jié)果。在程序的關(guān)鍵處,我都給出了較為詳細的注釋,這里就不再講述ASP的基本知識。讀者可以到陶吧ASP專欄查閱。當然,我希望你在自己的服務器上調(diào)試程序時,有不懂的地方,還是查查身邊的ASP技術(shù)手冊,看看對象、方法或函數(shù)的詳細語法試著修改,看看結(jié)果如何變化——這可是學習編程的一個好方法呢。 一、顯示調(diào)查問題(research.html) 調(diào)查問題的設計要依據(jù)實際情況,或講究實用性或講究趣味性,在網(wǎng)頁上顯示的風格也或樸素或活潑,本例中是筆者主頁上的一個關(guān)于“21世紀最重要的是什么?”的趣味調(diào)查,為說明問題,下面的代碼中省略了美觀修飾的代碼,你自己動手設計時完全可以使用表格等技巧美化調(diào)查問題的顯示。為了使提換蜾賴韃槭輩揮跋斕鼻耙趁媯絳蛑懈雋說魴麓翱詰姆槳浮?BR> researchindex.html: < html > < title >調(diào)查板測試< /title > < head > < !-- start:定義新開的窗口-- > < script language=javascript > < !-- var newWindow = null function OpenWindow(htmurl) { if (! newWindow || newWindow.closed) { newWindow = window.open(htmurl,"newwin","toolbar=no,resiza =no,scrollbars=no,width=400,height=280"); }else { newWindow.focus(); } } //-- > < /script > < !-- end:定義新開的窗口-- > < /head > < body > < !-- start:調(diào)查題目、選項 -- > < p >您認為21世紀最重要的是什么?< /p > < form method="POST" action="vote/select.asp" name="research" LANGUAGE="javascript" onSubmit="OpenWindow()" target="newwin" > < p align="left" > < br > < input type="radio" value="1" name="Options" >知識(知識就是力量)< br > < input type="radio" value="2" name="Options" >學歷(學歷社會沒有終結(jié))< br > < input type="radio" value="3" name="Options" >金錢(經(jīng)濟就是基礎)< br > < input type="radio" value="4" name="Options" >愛情(永不進入墳墓的愛情)< br > < input type="radio" value="5" name="Options" >理想(天啦,理想是什么)< br > < input type="radio" value="6" name="Options" >民主意識(關(guān)心政治)< br > < input type="radio" value="7" name="Options" >科學思想(科教興國)< br > < input type="submit" value="提交" name="voting" > < input type="button" value="查看" name="viewing" onClick="OpenWindow(vote/viewresult.asp)" > < /form > < !-- start:調(diào)查題目、選項 -- > < /body > < /html > 二、處理用戶選擇(select.asp) 依據(jù)上面的調(diào)查選項,我們得出記錄調(diào)查得票的數(shù)據(jù)庫researchdb.mdb的設計(以Access為例),表名為:research。如果你的數(shù)據(jù)庫和表沒有存為以上的名字,那么在后面的程序和操作中你不要忘記也相應修改過來。 存放調(diào)查投票數(shù)的表research: 字段名 數(shù)據(jù)類型 默認值 id 自動編號 1 select1 數(shù)字 0 select2 數(shù)字 0 select3 數(shù)字 0 select4 數(shù)字 0 select5 數(shù)字 0 select6 數(shù)字 0 select7 數(shù)字 0 建好數(shù)據(jù)庫后,我們來在服務器上建立數(shù)據(jù)源。首先,運行“控制面板”中的“ODBC”,選擇“系統(tǒng)DSN”,按“添加”按鈕,選取“Microsoft Access Driver”,選定后按“完成”按鈕,然后在ODBC設定中“數(shù)據(jù)源名”輸入框輸入數(shù)據(jù)庫名稱,本例中為researchdb,然后按下“選取”按鈕選擇數(shù)據(jù)庫文件(你不會說你忘記了剛剛設計的數(shù)據(jù)庫存在那里了吧),選定后按“確定”,你即可看到新建的數(shù)據(jù)源 researchdb。這樣,我們就可以在ASP中調(diào)用了。 select.asp: < % 下面的if語句是通過驗證selected是否為空來判斷用戶是否作了選擇 if request.form("options") < >Empty then % > < % 下面的if語句是通過比較Request的兩個collection(ServerVariables和Cookies)的值 來防止用戶連續(xù)按提交而影響調(diào)查的結(jié)果 if not Request.ServerVariables("REMOTE_ADDR")=request.cookies("IPAddress") then 把訪問客戶的IP信息寫入Cookies response.cookies("IPAddress")=Request.ServerVariables("REMOTE_ADDR") % > < % 建立連接(Connection)對象,打開記錄調(diào)查結(jié)果的數(shù)據(jù)庫 set conn=server.createobject("ADODB.CONNECTION") conn.open "researchdb" % > < % 定義變量 dim rs dim sql dim selected selected=request.form("options") 建立記錄集合(Recordset)對象,用方法Open打開對象,同時修改對應的數(shù)據(jù) set rs=server.createobject("adodb.recordset") 修改數(shù)據(jù)表ressearch中的數(shù)據(jù),即對應的投票數(shù)增加1 sql="update research set select"&selected&"=select"&selected&"+1 where id=1" rs.open sql,conn,3,3 從內(nèi)存中清除記錄集合對象 set rs=nothing 關(guān)閉連接 conn.close 從內(nèi)存中清除連接對象 set conn=nothing 連接到瀏覽調(diào)查結(jié)果的頁面 response.redirect "viewresult.asp" else Response.write "投票失敗提示:您剛才已投了票,謝謝您的支持!" end if else Response.write "投票失敗提示:您忘記選擇了!" end if % > 三、瀏覽調(diào)查結(jié)果(viewresult.asp) 本例中使用條形圖來直觀顯示調(diào)查結(jié)果,方法是用選項的投票數(shù)占總投票數(shù)的百分比值乘上5,得出的值作為條形圖http://www.okasp.com/techinfo/bar.gif顯示的寬(可用常見的圖象工具制作一個漸變色的小條形圖,或是到網(wǎng)上down一個)。為了給出帶兩為小數(shù)的得票的百分比,程序中使用了VBScript中的四舍五入函數(shù)Round。為了美觀,把調(diào)查結(jié)果放到表格中顯示,如圖所示。下面給出的代碼中略去了表格的設計。 viewresult.asp < % set conn=server.createobject("ADODB.CONNECTION") conn.open "researchdb" % > < % dim rs dim sql dim select1 dim select2 dim select3 dim select4 dim select5 dim select6 dim select7 dim total set rs=server.createobject("adodb.recordset") sql="select * from research where id=1" rs.open sql,conn,1,1 total=rs("select1")+rs("select2")+rs("select3")+ _ rs("select4")+rs("select5")+rs("select6")+rs("select7") 判斷總票數(shù)是否為0,確保下面的除法有效 if total > 0 then select1=(rs("select1")/total)*100 select2=(rs("select2")/total)*100 select3=(rs("select3")/total)*100 select4=(rs("select4")/total)*100 select5=(rs("select5")/total)*100 select6=(rs("select6")/total)*100 select7=(rs("select7")/total)*100 % > < p >謝謝您的參與,下面是當前的調(diào)查結(jié)果 < p > ◇知識: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select1*5)% > height=4 > < %=rs("select1")% >人 占:< %=round(select1,2)% >%< br > ◇學歷: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select2*5)% > height=4 > < %=rs("select2")% >人 占:< %=round(select2,2)% >%< br > ◇金錢: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select3)*5% > height=4 > < %=rs("select3")% >人 占:< %=round(select3,2)% >%< br > ◇愛情: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select4)*5% > height=4 > < %=rs("select4")% >人 占:< %=round(select4,2)% >%< br > ◇理想: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select5)*5% > height=4 > < %=rs("select5")% >人 占:< %=round(select5,2)% >%< br > ◇民主意識: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select6)%*5 > height=4 > < %=rs("select6")% >人 占:< %=round(select6,2)% >%< br > ◇科學思想: < img src=http://www.okasp.com/techinfo/bar.gif width=< %=int(select7)%*5 > height=4 > < %=rs("select7")% >人 占:< %=round(select7,2)% >%< /p > < p align="center" >已經(jīng)有:< %=total% >人參加調(diào)查< br >< br > 【< a href="javascript:window.close()" >關(guān)閉窗口< /a >】< /p > < p > < % else response.write "還沒有人參與調(diào)查" end if rs.close set rs=nothing conn.close set conn=nothing % > 附注: 以上代碼均在 WindowsNT4.0中文(Pack 6)/IIS4.0上調(diào)試通過。 以下任何一種環(huán)境都可執(zhí)行ASP: 一、Windows NT Server 4.0 / IIS3.0以上 二、Windows NT WorkStation 4.0 / Microsoft Peer Web Service3.0以上 三、Windows 95/98 / Microsoft Personal Web Server 1.0a以上
|