Using ASP to make personalized survey Board (with source program)

Source: Internet
Author: User
Tags modify odbc servervariables
Now, on the Internet to do a survey is very common, many commercial Web sites often have a variety of theme of the survey Board, some commercial sites are free to provide users with research boards, such as: Broad (poll.bodachina.com). Because of the popularity of the network, online surveys tend to achieve better results. In their own personal site to put a fun problem investigation board, indeed for the site Tim a lot of, and through the results of the survey you can more accurately understand the views of users on their site. As a webmaster, if your site also needs to investigate a topic, and you hate to use someone else's free board (free often comes at a price, such as advertising!). , then I suggest you take more than 10 minutes to read this article about using ASP to make a survey board. All you have to do is copy the code from the text to your machine and make a little change, and you immediately have a personalized survey board that belongs to you. All right, get up and look down.
The survey board has three files: Displays survey questions (research.html), handles user selection (select.asp), and browses survey results (viewresult.asp). The design idea is: The ASP obtains by the form the information, and accordingly modifies the record investigation obtains the ticket number the database, then the ASP reads the database, obtains each survey question the number of votes, passes the number of votes to adjust the corresponding bar chart display the width to be intuitive proportional to give the survey result. At the key point of the program, I have given a more detailed comment, here is no longer the basic knowledge of ASP. Readers can go to the Tao Bar ASP column to check. Of course, I would like you to debug the program on your own server, there is no understanding of the place, or check the side of the ASP technology Manual, look at the object, method or function of the detailed syntax to try to modify, see how the results change-this is a good way to learn programming.

I. Display of survey questions (research.html)

Survey problem design to be based on the actual situation, or pay attention to practicality or interesting, the style displayed on the Web page or simple or lively, this example is the author on the home page on the "21st century the most important thing?" "Interesting survey, to illustrate the problem, the following code omitted the beautifully decorated code, you can use the design of the table and other techniques to beautify the display of investigation problems. In order to submit or browse the survey without affecting the current page, the program is given a popup new window scheme.
Researchindex.html:

< HTML >
< title > Survey Board Test </title >
< head >
<!--start: Defining a newly opened window-->
< script Language=javascript >
<!--
var NewWindow = null
function Openwindow (Htmurl)
{
if (! NewWindow | | newwindow.closed)
{
NewWindow =
window.open (Htmurl, "Newwin", "toolbar=no,resizable=no,scrollbars=no,width=400,height=280");
}else
{
Newwindow.focus ();
}
}
-->
</script >
<!--end: Defining a newly opened window-->
< BODY >
<!--Start: Survey topics, Options->
< p > What do you think is the most important thing in 21st century? </p >
< form method= "POST" action= "vote/select.asp" name= "It" language= "JavaScript"
target= "Newwin" >
< P align= "left" >
< br >
< input type= "Radio" value= "1" name= "Options" > Knowledge (Knowledge is Power) < br >
< input type= "Radio" value= "2" name= "Options" > Education (Education Society does not end) < br >
< input type= "Radio" value= "3" name= "Options" > Money (Economy is Foundation) < br >
< input type= "Radio" value= "4" name= "Options" > Love (Love never Goes to the grave) < br >
< input type= "Radio" value= "5" name= "Options" > Ideals (God, what is the ideal) < br >
< input type= "Radio" value= "6" name= "Options" > Democratic Consciousness (Care politics) < br >
< input type= "Radio" value= "7" name= "Options" > Scientific ideas (Rejuvenating the country) < br >
< input type= "submit" value= "submitted" name= "voting" >
< input type= "button" value= "View" name= "Viewing" >
</form >
<!--Start: Survey topics, Options->
</body >

Second, the Processing user choice (select.asp)

Based on the above survey options, we draw the design of the database Researchdb.mdb that records the survey's votes (in Access, for example), and the table is named: Research. If your database and tables do not exist in the above name, then in the following procedures and operations, you should not forget to modify the corresponding.

Research on the table that holds poll numbers:

Field name data type default value
ID Auto Number 1
Select1 number 0
Select2 number 0
SELECT3 number 0
SELECT4 number 0
SELECT5 number 0
SELECT6 number 0
SELECT7 number 0


After the database has been built, we will set up a data source on the server. First, run ODBC in Control Panel, select System DSN, press the Add button, choose Microsoft Access Driver, press Finish when selected, and then enter the database name in the data Source name input box in ODBC settings. In this case, RESEARCHDB, and then press the Select button to select the database file (you do not say you forget the database you just designed is there), select and press "OK", you can see the new data source researchdb. In this way, we can call it in ASP.
Select.asp:

<%
' The following if statement determines whether the user has made a choice by verifying that the selected is null
If Request.Form ("Options") < >empty Then
% >

<%
' The following if statement is the value of the two collection (ServerVariables and cookies) that are compared to the request
' To prevent users from continuing to influence the results of the survey by submitting
If not Request.ServerVariables ("REMOTE_ADDR") =request.cookies ("IPAddress") Then
' Write the IP information of the client to the cookie
Response.Cookies ("IPAddress") =request.servervariables ("REMOTE_ADDR")
% >
<%
' Establish a connection (Connection) object to open the database that records the results of the survey
Set Conn=server.createobject ("ADODB. CONNECTION ")
Conn.Open "Researchdb"
% >

<%
' Define variables
Dim rs
Dim sql
Dim selected
Selected=request.form ("Options")
' Set up a record collection (Recordset) object, open the object with the method open, and modify the corresponding data
Set Rs=server.createobject ("Adodb.recordset")
' Modify data in Datasheet ressearch, that is, increase the number of votes by 1
Sql= "Update" set Select &selected& "=select" &selected& "+1 where id=1"
Rs.Open sql,conn,3,3
' Clears the Record collection object from memory
Set rs=nothing
' Close the connection
Conn.close
' Clears the connection object from memory
Set conn=nothing
' Connect to the page that browses the survey results
Response.Redirect "Viewresult.asp"
Else
Response.Write "Vote failure Tip: You have just voted, thank you for your support!" "
End If
Else
Response.Write "Vote failure Tip: you forgot to choose!" "
End If
% >

Iii. Browse the results of the survey (viewresult.asp)

In this example, a bar chart is used to visually display the results of the survey by multiplying 5 by the percentage of the voting number of the option to the total number of votes, and the resulting value as a bar chart http://edu.cnzz.cn/NewsInfo/ The width of the bar.gif display (use common image tools to make a small bar of the gradient color, or down one on the Internet). In order to give a percentage of the votes with two decimal digits, the rounding function in VBScript is used in the program round. For the sake of aesthetics, put the results of the survey into the table as shown in the figure. The design of the table is omitted from the code given below.

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 the Where id=1"
Rs.Open sql,conn,1,1
Total=rs ("Select1") +rs ("Select2") +rs ("Select3") +rs ("Select4") +rs ("Select5") +rs ("Select6") +rs ("Select7")
' Judge whether the total number of votes is 0, to ensure that the following division is valid
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 > Thank you for your participation, the following is the current survey results
< p >
◇ Knowledge:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (select1*5)% > Height=4 >
<%=rs ("Select1")% > People:<%=round (select1,2)% >%< br >
◇ Education:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (select2*5)% > Height=4 >
<%=rs ("Select2")% > People:<%=round (select2,2)% >%< br >
◇ Money:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (select3) *5% > Height=4 >
<%=rs ("select3")% > People:<%=round (select3,2)% >%< br >
◇ Love:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (select4) *5% > Height=4 >
<%=rs ("Select4")% > People:<%=round (select4,2)% >%< br >
◇ Ideal:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (select5) *5% > Height=4 >
<%=rs ("Select5")% > People:<%=round (select5,2)% >%< br >
◇ Democratic Consciousness:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (SELECT6)%*5 > Height=4 >
<%=rs ("SELECT6")% > People:<%=round (select6,2)% >%< br >
◇ Scientific thought:
< img src=http://edu.cnzz.cn/newsinfo/bar.gif width=<%=int (select7)%*5 > Height=4 >
<%=rs ("select7")% > People:<%=round (select7,2)% >%</p >

< p align= "center" > has:<%=total% > People participate in the investigation < br >< br >
"< a href=" Javascript:window.close () > Close window </a > "</p >
< p >
<%
Else
Response.Write "No one's been involved in the investigation."
End If
Rs.close
Set rs=nothing
Conn.close
Set conn=nothing
% >

Note:
The above code is debugged on WindowsNT4.0 Chinese (Pack 6)/iis4.0.
Any of the following environments can perform asp:
One, Windows NT Server 4.0/iis3.0 above
Second, Windows NT WorkStation 4.0/microsoft Peer Web Service3.0 above
Third, Windows 95/98/microsoft Personal Web Server 1.0a above



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.