With the popularity of the Internet, online surveys can often achieve better results. Putting an interesting question survey board on your personal website can really add a lot of color to the website; and through the survey results, you can learn more and more accurately about netizens' opinions on your website. As a Webmaster, if your website also needs to conduct surveys on a certain topic, and you hate using survey boards provided by others for free (free often comes with a price, such as advertising!), then I suggest you spend ten minutes Time to read this article introducing the use of ASP to create a survey board. You only need to copy the code in the article to your machine, make slight modifications, and you will immediately have a personalized survey board of your own. Okay, cheer up and look down.
There are three files in this survey board: displaying survey questions (research.html), processing user selections (select.asp), and viewing survey results (viewresult.asp). The design idea is: ASP obtains the information sent from the form, and accordingly modifies the database that records the number of votes in the survey. Then ASP reads the database, obtains the number of votes for each survey question, and adjusts the corresponding bar chart display based on the number of votes. The survey results are presented in an intuitive and proportional manner. I have given more detailed comments on the key points of the program, and I will not go into the basic knowledge of ASP here. Readers can check it out in the Taoba ASP column. Of course, I hope that when you debug the program on your own server, if you don’t understand something, you should check the ASP technical manual around you to see the detailed syntax of objects, methods or functions, try to modify it, and see how the results change—— This is a great way to learn programming.
1. Display survey questions (research.html)
The design of survey questions should be based on the actual situation, either practical or interesting, and the style displayed on the web page can be simple or lively. In this example, it is an about question on the author’s homepage. An interesting survey on "What is the most important thing in the 21st century?" In order to illustrate the problem, the code for aesthetic modification is omitted in the following code. When you design it yourself, you can use tables and other techniques to beautify the display of survey questions. In order to make Ti change the dragon? The Lai Ta maples waved their nose rake and took advantage of the Gui? The tape was slack? Jun said?
researchindex.html:
< html >
<title>Survey Board Test</title>
<head>
< !-- start: Define a new window-- >
<script language=javascript>
< !--
var newWindow = null
functionOpenWindow(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: Define the newly opened window-->
</ /head >
<body>
< !-- start: survey questions, options -- >
< p >What do you think is the most important thing in the 21st century? < /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" >Knowledge (knowledge is power)< br >
< input type="radio" value="2" name="Options" >Academic qualifications (the academic society has no end)< br >
< input type="radio" value="3" name="Options" >Money (economy is the basis)< br >
< input type="radio" value="4" name="Options" >Love (love that will never enter the grave)< br >
< input type="radio" value="5" name="Options" >Ideal (gosh, what is an ideal)< br >
< input type="radio" value="6" name="Options" >Democratic awareness (concern about politics)< br >
< input type="radio" value="7" name="Options" >Scientific Thought (rejuvenating the country through science and education)< br >
< input type="submit" value="submit" name="voting" >
< input type="button" value="View" name="viewing" onClick="OpenWindow(vote/viewresult.asp)" >
< /form>
< !-- start: survey questions, options -- >
< /body >
< /html >
2. Processing user selection (select.asp)
Based on the above survey options, we come up with the design of the database researchdb.mdb (taking Access as an example) that records survey votes. The table name is: research. If your database and tables are not saved with the above names, don't forget to modify them accordingly in subsequent procedures and operations.
Table research that stores survey vote numbers:
field name data type default value
id automatic number 1
select1 number 0
select2 number 0
select3 number 0
select4 number 0
select5 number 0
select6 number 0
select7 number 0
After building the database, let's create a data source on the server. First, run "ODBC" in the "Control Panel", select "System DSN", press the "Add" button, select "Microsoft Access Driver", press the "Finish" button after selection, and then click "Data Source" in the ODBC settings. Enter the name of the database in the "Name" input box, in this case researchdb, then press the "Select" button to select the database file (you won't say that you forgot where the database you just designed exists), and press "OK" after selecting it. You can see the newly created data source researchdb. In this way, we can call it in ASP.
select.asp:
< %
The if statement below is to determine whether the user has made a selection by verifying whether selected is empty.
if request.form("options") < >Empty then
% >
< %
The following if statement compares the values of the two collections (ServerVariables and Cookies) of the Request to prevent the user from continuously pressing submit and affecting the results of the investigation.
if not Request.ServerVariables("REMOTE_ADDR")=request.cookies("IPAddress") then
Write the visiting customer’s IP information into Cookies
response.cookies("IPAddress")=Request.ServerVariables("REMOTE_ADDR")
%>
<%
Establish a Connection object and open the database that records the survey results.
set conn=server.createobject("ADODB.CONNECTION")
conn.open "researchdb"
% >
< %
Define variables
dimrs
dimsql
dim selected
selected=request.form("options")
Create a Recordset object, open the object using the Open method, and modify the corresponding data at the same time.
set rs=server.createobject("adodb.recordset")
Modify the data in the data table research, that is, increase the corresponding number of votes by 1
sql="update research set select"&selected&"=select"&selected&"+1 where id=1"
rs.open sql,conn,3,3
Clear the record collection object from memory
set rs=nothing
close connection
conn.close
Clear connection object from memory
set conn=nothing
Connect to page to browse survey results
response.redirect "viewresult.asp"
else
Response.write "Voting failure reminder: You have just voted, thank you for your support!"
end if
else
Response.write "Voting failure prompt: You forgot to select!"
end if
% >
3. Browse survey results (viewresult.asp)
In this example, a bar chart is used to visually display the survey results. The method is to multiply the percentage value of the number of votes for the option to the total number of votes by 5, and the resulting value is used as a bar The width of the bar.gif display (you can use common image tools to create a small gradient bar chart, or download one online). In order to give the percentage of votes with two decimal places, the rounding function Round in VBScript is used in the program. For the sake of beauty, the survey results are displayed in a table, as shown in the figure. The design of the table has been omitted from the code given below.
viewresult.asp
<%
set conn=server.createobject("ADODB.CONNECTION")
conn.open "researchdb"
%>
<%
dimrs
dimsql
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")
Determine whether the total number of votes is 0 and 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. Below are the current survey results.
<p>
◇Knowledge:
< img src=bar.gif width=< %=int(select1*5)% > height=4 >
< %=rs("select1")% >Period: < %=round(select1,2)% >%< br >
◇Education:
< img src=bar.gif width=< %=int(select2*5)% > height=4 >
< %=rs("select2")% >Period: < %=round(select2,2)% >%< br >
◇Money:
< img src=bar.gif width=< %=int(select3)*5% > height=4 >
< %=rs("select3")% >Period: < %=round(select3,2)% >%< br >
◇Love:
< img src=bar.gif width=< %=int(select4)*5% > height=4 >
< %=rs("select4")% >Period: < %=round(select4,2)% >%< br >
◇Ideal:
< img src=bar.gif width=< %=int(select5)*5% > height=4 >
< %=rs("select5")% >Period: < %=round(select5,2)% >%< br >
◇Democratic consciousness:
< img src=bar.gif width=< %=int(select6)%*5 > height=4 >
< %=rs("select6")% >Period: < %=round(select6,2)% >%< br >
◇Scientific Thought:
< img src=bar.gif width=< %=int(select7)%*5 > height=4 >
< %=rs("select7")% >People: < %=round(select7,2)% >%< /p >
< p align="center" >Already: < %=total% > people participated in the survey < br >< br >
【< a href="javascript:window.close()" >Close window</a>】</p>
<p>
<%
else
response.write "No one has participated in the survey yet"
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
Note:
The above codes were debugged on WindowsNT4.0 Chinese (Pack 6)/IIS4.0.
ASP can be executed in any of the following environments:
1. Windows NT Server 4.0 / IIS3.0 or above 2. Windows NT WorkStation 4.0 / Microsoft Peer Web Service 3.0 or above 3. Windows 95/98 / Microsoft Personal Web Server 1.0a or above