Using ASP to do an online survey

Source: Internet
Author: User
Tags exit functions include join modify connect table name administrator password
On the online survey you will not be unfamiliar with it, give a question and several answers, let users fill out, and then save the results to the database, automatic statistics, and finally give a statistical figure. This period of learning to do with me to do an online survey system.

First, the function design

This simple system also needs to do functional design? Some people may feel strange, but anyway, regardless of how the system, the first functional design can always have a comparative understanding of the system. Let's take a look at the function of the online survey. The basic function has already said, is to give a question and several answers, then statistics, finally gives the graph. On this basis, we can consider adding a time period (validity period) to a survey, in which the investigation is effective, and after that time, the survey is automatically concluded. In addition, we can specify that a user can submit only one answer at a time. If you want to limit more, you can assign an IP can only submit an answer once, however, this may be a person in the Internet café can only have one submitted. Some of the problems in the survey may be a single choice, and some may be multiple-choice. Finally, the statistics chart, in the statistical chart to give the answer, the number of votes for each answer, and to show the number of votes in each answer percentage. The general use of horizontal map on it, but also relatively easy to achieve, of course, if you want to change to vertical, you can.

Now based on the above summary of the online survey functions are as follows:

1, the data is kept in Access 2000 database;

2. One visit per user can vote once

3, give the statistics of each survey, with a statistical map to show

4, each investigation has an expiration date, automatically end after expiration. The completed survey can only view the results.

5, the administrator can increase the investigation, modify the investigation of the answer (add, modify, delete, modify type).

6. For the completed investigation, the administrator can only delete the investigation, but not the answer.

7, only one administrator (single user)

Second, database design

Now to design the database, according to functional requirements, at least three tables, one is the administrator table, the second is the questionnaire, the third is the survey results table. The database file name is Survey.mdb can be changed to. asp if changed, please make the corresponding modification in the ASP program.

Table A, Administrator table table name: Manage
-----------------------------------------------------------------
Field type length description
-----------------------------------------------------------------
MANAGE_ID AutoNumber-No use here, future expansion
Manage_username Text 15 Administrator user name
Manage_password Text 15 Administrator password
-----------------------------------------------------------------

Create the Manage table and add a new record, fill in your admin username and password, and fill in the Xmxoxo

Table II, Questionnaire table name: survey
-----------------------------------------------------------------
Field type length description
-----------------------------------------------------------------
SURVEY_ID automatic numbering-increments, primary keys, indexed no Duplicates
Survey_question Text 255 Survey questions
Survey_type-Type, no: Radio is: multiple selection
Survey_stime Date-long date, start time
Survey_etime Date-long date, end time
-----------------------------------------------------------------

Table III, survey end table name: Survey_vote

-----------------------------------------------------------------
Field type length description
-----------------------------------------------------------------
Vote_no automatic numbering-increments, primary keys, indexed no Duplicates
vote_id long integral type-index has duplicate, decimal digit 0
Vote_answer Text 100 Survey answers
Vote_count Long integer-Number of votes
-----------------------------------------------------------------

Where the ID field of the Survey_vote table and the survey table has a many-to-many relationship. It does not have to be a relationship, but building relationships can make the idea clearer.

Iii. include documents

Here to use the function is not much, mainly to the database operation, if you want to prevent the input of HTML and other code, directly with Server.HTMLEncode processing can be, so do not need a special function to deal with. We can use the previous "Follow Me" series "Follow me to learn to do tree menu" in the inclusion file.

Shared function file, filename: inc.asp

<%
''*******************************************************************
' Common Database ASP functions
''*******************************************************************
' Database constants
Databasename= "Survey.mdb" "Database name, if renamed, here to modify the line
''*******************************************************************
"Open the Database
Sub Opendb (Connect)
Set Connect=server. CreateObject ("Adodb.connection")
Connect. connectionstring= "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= "&_
Server. MapPath (DatabaseName)
Connect. Open strconn
End Sub
''*******************************************************************
"Close the database
Sub Closedb (Connect)
Connect.close
Set connect=nothing
End Sub
''*******************************************************************
' Open a single table read
Sub OpenTable (connect,tbname,myrs)
Set Myrs=server.createobject ("Adodb.recordset")
Rssql= "SELECT * from" & Tbname
Myrs.open rssql,connect,1,1
End Sub
''*******************************************************************
' Close temporary table '
Sub CloseTable (RS)
Rs.close
Set rs=nothing
End Sub
''*******************************************************************
"Query the Database
Sub Searchtable (CONNECT,SQL,RS)
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open sql,connect,1,1
End Sub

''*******************************************************************
' Query and change the database
Sub Changetable (CONNECT,SQL,RS)
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open sql,connect,1,3
End Sub

''*******************************************************************
' Display information for debugging
Sub W (msg)
Response.Write msg
End Sub

''*******************************************************************
' Program interrupts for debugging
Sub Userstop ()
Response.End
End Sub
%>

Iv. Document Design

According to the traditional idea, we always design the online survey into three parts, one is to show the questionnaire, second, is to show the results of survey statistics, the third is the background management. In practical applications, we will find that the display of questionnaires is often in a Web page, the display results are usually a pop-up window. Background management is turned to through another link or login form. To make it easier to use online surveys, we write a JS script that shows the questionnaire section so that the Web page that needs to display the questionnaire is freely invoked by referencing the script. Okay, look at the file design.

1, inc.asp contains files. The main function library

2, surveycode.asp display questionnaire procedures. Use a script in the main page to call it.

3, survey.asp questionnaire list program. Lists the status of all surveys.

4, survey_vote.asp Display survey results procedures. The problem with the survey is represented by the parameter.

5, survey_manage.asp management procedures.

Here, we skip the page design, because style design is not what we want to say, so the style of the Web page, layout, CSS and so on, please design their own.

V. Documentation process

Write out the main process of the program, can be more convenient for later modification and expansion, transplantation. What's more important here is what subroutines to write in the program, and how to arrange the subroutines.

1, surveycode.asp Display survey form

<%
' Get querystring parameter, ID indicates survey number
' Judging the correctness of the parameters
"Judge whether the investigation is in the validity period.
' Read survey questions, types
' Output survey answers, and generate survey forms
' Close database and table
%>

2, survey.asp Show all investigation status

<%
' Read the database
%>
' Show all survey status and add links

3, survey_vote.asp display statistical results.

Here are two features, one is not submitted to the display, the second is to submit the answer after the statistics, and then display the results. If you do not take parameters, this is the first way. can also be divided into two files to complete.

<%
' Get the parameters. ID indicates survey number all data is from form
"To determine whether there are parameters, there are first statistics
"No, it's directly displayed.
' Statistics subroutine
%>
' Show subroutine

4, survey_manage.asp management procedures.

The management part is more complicated, to realize more functions. Let's first list the functions of management:

1) Admin login. Managed only after logging in

2 Exit Login. Secure exit after completion of administration.

The management of the survey is:

3) Add a survey. and increase the answer to the investigation.

4) Modify a survey. Modify content, time, type, investigate the content of the answer, add, delete

5) Delete a survey. You cannot delete an ongoing investigation.

To design its process for these functions

<%
' Get the parameters. Action represents actions, respectively, corresponding to the above functions.
"Move to the appropriate subroutine according to the action
' Login subroutine
' Exit Login subroutine
' Execute the increase in investigation questions subroutine
"Execute the increase survey answer subroutine
"To modify the questions and answers of the survey subroutine
' Perform the delete survey question subroutine
' Execute Delete survey Answer subroutine
<%
"To determine whether to log in, not to display the login form
"Show the form according to the action
' Show all survey subroutines
"Displays a single survey subroutine. Questions and Answers show together
"Displays an increase in the survey subroutine.
"Show Login Form
%>

Vi. Code-writing

After doing the process design, the writing code is more organized. Let's start with the simple. In writing code
Before, we have to enter some records in the database in order to do the test. First, add a survey question, and a few
Investigate the answers and enter some statistics manually.

We're going to write the surveycode.asp that shows the survey form. This file is called on other pages, so we're writing JS and VBS in a mixed way. When called, you can put it in a table with the following statement:

<script language= "JavaScript" src= "surveycode.asp?id=1" ></SCRIPT>

According to the above process, before displaying the form, we must first determine whether the investigation exists, whether it is in progress. In addition, to submit a hidden parameter in the form to indicate the problem number (ID) of the survey, the answer is submitted with the number of the answer Vote_no

FileName surveycode.asp

<!--#include file= "inc.asp"-->
<%
Id=request.querystring ("id")
If id<> "then" if there are parameters
Opendb my ' join database
Sql= "SELECT * from survey where survey_id=" & ID "query statement
searchtable my,sql,rs ' query database
If not rs.eof then ' if there is a record of this investigation
Question=rs ("survey_question") ' Read the question
Surveytype=rs ("Survey_type") ' read out the answer type
Stime=rs ("Survey_stime") ' read out start time
Etime=rs ("Survey_etime") ' read out end time
CloseTable rs ' Close table
If Stime<now () and Etime>now () then ' if the investigation is in progress
"Output survey form below
' First output forms and issues, form submission to survey_vote.asp
%>
document.write ("<form action= ' survey_vote.asp ' target= '" _blank ' "' method= '" ' Post ' > ");
document.write ("<table border= ' 1", "cellpadding=", "cellspacing=0" ' bordercolorligh= ', ' #000000 ');
document.write ("bordercolordark= ' #ffffff ' width= '" 100% ' align= ' center ' ><tbody> ");
document.write ("<tr><td colspan= ' 2" ' align= ' center ' ><b><%=server.htmlencode (question)% ></b></td></tr> ");
<%
Sql= "Select Vote_no,vote_answer from Survey_vote where vote_id=" &id "Query answers SQL
Searchtable my,sql,rs ' Execute query
If not rs.eof then ' if there is an answer, output
For I=1 to Rs.recordcount
%>
document.write ("<tr><td align= ' right ' ><input name= ' res ' type= '");
<%
If Surveytype then ' judgment type, show single selection or multiple selection
%>
document.write ("checkbox");
<%else%>
document.write ("Radio");
<%end if ' The text of the output answer and the value submitted (VOTE_NO)%>
document.write ("' Value=<%=rs" ("Vote_no")%>></td><td><%=rs ("Vote_answer")%></td ></tr> ");
<%
Rs.movenext
Next
The following sentence outputs a hidden parameter, passing the problem number (ID)
"and use a JS function to define the link after clicking the view
%>
document.write ("<tr><td colspan= ' 2" "align= ' center ' ><input ' type= '" ", ' hidden '" id ' name= ' ") <%=id%> ' > ');
document.write ("<input type=" "Submit" ' Class=button value= ' vote ' > ');
document.write ("<input Type=button Class=button value=" "View" ' > ");
document.write ("</td></tr></tbody></table></form>");
function jump (ID) {
window.open ("survey_vote.asp?id=" +id, "survey")
}
<%
End If
End If
End If
CloseTable RS
Closedb my
End If
%>

Upon completion of the surveycode.asp, the following points have been identified in our implementation:

1, in Survey_vote.asp, if the QueryString parameter ID has a value, then view the result;

2, in the survey_vote.asp, if the form parameter ID has a value, you must first statistics;

3, in survey_vote.asp, the submitted form parameter res is the number of the answer vote_no;

VII. Statistical results

First, we will complete the display of statistical results with the most closely related to surveycode.asp survey_vote.asp file. At the end of the previous article, we have explained some of the parameters identified in the surveycode.asp.

Statistical results survey_vote.asp

<!--#include file= "inc.asp"-->
<title> Survey Statistics Results </title>
<link rel= "stylesheet" href= "Main.css" type= "Text/css" >
<body>
<%
The previous sentence first joins the include file, referencing the function.
Id=request.querystring ("id") ' gets querystring parameter ID
Opendb my ' Connect to the database
If id= "" then "if not, it is not directly to see the result
Id=request.form ("id") ' Gets the form parameter ID
If id<> "then" if there is a value, it is to be counted first
Surveycount () ' Call the statistics subroutine
End If
End If
If id<> "" Then
Disp_survey () "No matter what kind, the final display results
End If
Closedb my ' shut down the database

"-----Statistic subroutine-----
Sub Surveycount ()
If session ("SURVEY_OK") = "then" if not voted
No=request.form ("res") ' Gets the number of the answer
If no<> "" Then
"Define the SQL statement so that the number of answers submitted is +1
sql= "Update survey_vote set vote_count=vote_count+1 where vote_no= in (" & No & ")"
My.execute SQL
End If
Session ("SURVEY_OK") = "OK"
End If
End Sub
''------------------

'---Display the result subroutine---
Sub Disp_survey ()

"Define the SQL statement and get the questions investigated
Sql= "Select Survey_question from Survey where survey_id=" & ID
Searchtable my,sql,rs ' Execute query
Question=rs ("Survey_question") "to save the problem in the question
CloseTable rs ' Close table
"Define the SQL statement, and get the sum of the numbers of answers.
Sql= "Select sum (vote_count) as total from Survey_vote where vote_id=" & ID
Searchtable My,sql,rs
Total=rs ("Total")
CloseTable rs ' Close table

"Define the SQL statement and get all the answers to the text part and the number of votes
Sql= "Select Vote_answer,vote_count from Survey_vote where vote_id=" & ID
Searchtable my,sql,rs ' Execute query
"Use the table to output the tables below.
%>
<table width= "border=" 1 "align=" center "cellpadding=" 2 "cellspacing=" 0 "
Bordercolorligh= "#000000" bordercolordark= "#ffffff" >
<tr>
&LT;TD colspan= "4" align= "Center" ><b> Survey results </b></td>
</tr>
<tr>
&LT;TD colspan= "4" ><b> survey questions:<%=question%></b></td>
</tr>
<tr >
&LT;TD width= "align=" height= "center" > Answer </td>
&LT;TD width= "align=" height= "center" > Voter turnout </td>
&LT;TD width= "align=" "center" height= > ratio </td>
&LT;TD width= "align=" center "height=" > Votes </td>
</tr>
<%do While not rs.eof
If Total=0 Then
Percent=0 ' If no one votes, the percentage is 0
Else
Percent=int (RS ("Vote_count")/total*10000)/100 ' Calculate percent
End If
%>
<tr>
&LT;TD width= "align=" "Center" ><%=rs ("Vote_answer")%></td>
&LT;TD width= "align=" "Left" >
<table border= "0" width= "<%=percent%>" bgcolor= "#CCCC00" height= "ten" >
<tr>
<td></td>
</tr>
</table>
</td>
&LT;TD width= "align=" "Center" ><%=percent%>%</td>
&LT;TD width= align= "center" ><%=rs ("Vote_count")%></td>
</tr>
<%
Rs.movenext
Loop
%>
<tr>
&LT;TD colspan= "4" > To <%=now ()%>, total <%=total%> vote
<a href= "Javascript:window.close ()" > Close window </a>
</td>
</tr>
</table>
<%
CloseTable rs ' Close table
End Sub
''------------------
%>
</body>

In the display of the voting process, we use the session variable SURVEY_OK to indicate whether the vote has been voted. In addition, this shows the statistics, referencing the CSS file to control the style of the table, you can according to their own request to join.

Viii. listing the status of all investigations

Now that we're done with survey.asp, its main task is to list all the survey statuses, including:

1, the investigation of the problem, linked to the voting form page (directly written on this page);

2, the starting time of the investigation;

3, the end of the investigation time;

4, the investigation of the status: Not started, in progress, has ended;

5. Number of votes in the survey;

6, the type of investigation, radio or multiple election;

7, also give a link to see the results of the poll;

According to these requirements, the corresponding table can be queried, some statements, such as the total number of votes, SQL statements in fact, in the above survey_vote.asp has been written.

List status of all surveys survey.asp


<!--#include file= "inc.asp"-->
<title> online survey list </title>
<link rel= "stylesheet" href= "Main.css" type= "Text/css" >
<body>
<%
Id=request.querystring ("id") ' gets the parameter
If id<> "then" displays this survey form if there are parameters
Response.Write ' <script language= ' JavaScript ' src= ' surveycode.asp?id= ' &id& ' ></SCRIPT> '
Else ' Otherwise called subroutine display state
Disstat ()
End If

'-----Show the Status subroutine----
Sub Disstat ()
Opendb my ' Connect to the database
OpenTable My, "survey", RS ' directly open the table
"Show each record in a table below
"First show the table header
%>
<table width= "760" border= "1" cellspacing= "0" cellpadding= "2"
align= "center" bordercolorligh= "#000000" bordercolordark= "#ffffff" >
<tr>
&LT;TD colspan= "8" align= "Center" ><b> online survey list </b></td>
</tr>
<tr >
&LT;TD width= "align=" center "height=" > Number </td>
&LT;TD width= "align=" center "height=" > Survey questions </td>
&LT;TD width= "align=" center "height=" > Type </td>
&LT;TD width= "140" align= "center" height= "> Start time </td>
&LT;TD width= "140" align= "center" height= "> End time </td>
&LT;TD width= "align=" "Center" height= "> Status </td>
&LT;TD width= "align=" "Center" height= "> voted number </td>
&LT;TD width= "align=" center "height=" > View </td>
</tr>
<%
"Output each record below
Do as not rs.eof
"read out each field first
Id=rs ("survey_id")
Question=rs ("Survey_question")
"read out type"
If RS ("Survey_type") Then
Stype= "Multiple Selection"
Else
Stype= "Single Selection"
End If
Stime=rs ("Survey_stime")
Etime=rs ("Survey_etime")
' Judging state
If now () <stime Then
Stat= "Not Started"
Else
If Now<etime Then
Stat= "In Progress"
Else
stat= "Closed"
End If
End If

"Define the SQL statement, and get the sum of the numbers of answers.
Sql= "Select sum (vote_count) as total from Survey_vote where vote_id=" & ID
Searchtable My,sql,tmprs ' Query
Total=tmprs ("Total")
closetable Tmprs ' Close table
"Output a record below
%>
<tr >
&LT;TD align= "center" height= "><%=id%></td>"
&LT;TD height= ">"
<a href= "Survey.asp?id=<%=id%>" ><%=question%></a>
</td>
&LT;TD align= "center" height= "><%=stype%></td>"
&LT;TD align= "center" height= "><%=stime%></td>"
&LT;TD align= "center" height= "><%=etime%></td>"
&LT;TD align= "center" height= "><%=stat%></td>"
&LT;TD align= "center" height= "><%=total%></td>"
&LT;TD align= "center" height= ">"
<a href= "survey_vote.asp?id=<%=id%>" target= "_blank" > View </a>
</td>
</tr>
<%
Rs.movenext ' Move to the next, loop
Loop
%>
</table>
<%
CloseTable rs ' Close table
Closedb my ' shut down the database
End Sub
''----------------------
%>
</body>

Nine, backstage management

In the admin page survey_manage.asp, we've listed the management features that it's going to implement. The management process is to show all the investigation first, for the investigation has not started, you can modify, delete, for the completed investigation, you can delete, cannot modify, for the ongoing investigation can only modify its end time. The action is represented by a parameter action, meaning the following:

1, no parameters. Represents the first entry, displaying the login form

2, login means to perform login

3, logout sign to perform exit login

4, Showaddquestion said to show an increase in the survey

5, Showsurvey show a survey

6, Doaddsurvey said that the implementation of an additional investigation

7, Doaddanswer said the implementation of an additional answer

8, Dodelsurvey said to delete a survey

9, Dodelanswer said to delete an answer

10, Domodify said to revise a survey and answer



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.