With the popularization of the network, online survey can often 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,resiza =no,scrollbars=no,width=400,height=280");
}else
{
Newwindow.focus ();
}
}
-->
/script >
!--End: Defining a newly opened window--
/head >
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=" "The" "JavaScript" language= "
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 (Academic Education Society does not end)
"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" the ideal (God, what is the ideal)
Input type= "Radio" value= "6" name= "Options" "democratic consciousness (concern politics)" BR "
Input type= "Radio" value= "7" name= "options" > > Scientific thought (rejuvenating by science and education)
"Submit" value= "Submission" name= "voting" of "input type="
Input type= "button" value= "View" name= "viewing"
/form >
!--Start: Survey topics, Options--
/body >
/html >
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 empty
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"
% >
%
Defining variables
Dim rs
Dim sql
Dim selected
Selected=request.form ("Options")
Create a Record collection (Recordset) object, open the object with 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
Purge record collection objects from memory
Set rs=nothing
Close connection
Conn.close
To clear a Connection object from memory
Set conn=nothing
Connect to a page that browses 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://www.163design.net/a/y/ 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")
Determine 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"