Published: Saturday, March 27, 1999
Creating an On-Line Quiz Using ASP
By Ryan S.
If you have read this
message board post, you will see that someone asked how
to create a multi-question quiz. Here is my response, in tutorial form.
First, it depends if you will be using this quiz format over - eg, a weekly
quiz. I'll assume that you will. If you haven't noticed from my other posts, I'm
extremely pro-database. Let's create a simple database called myDatabase.mdb
(assuming its MS Access). Then create a table called Questions. Now, create
these records. Also, I'll assume that its like a book type quiz. If
the user misses the question, it shows them where in the book they can find the
information.
|
QuestionID | AutoNumber |
| Question | Text 255 |
| AnswerA | Text 255 |
| AnswerB | Text 255 |
| AnswerC | Text 255 |
| AnswerD | Text 255 |
| CorrectAns | Number 1 (the reason it's number is because you say which is
correct. AK - 1 = A, 2 = B, etc) |
| Reference | Text255 |
Now, fill in your questions (probably using a form in Access, which is
easiest). Now for the ASP
I'm assuming that you have put in your <HTML>, <HEAD>, and <BODY>. This goes
directly below <BODY>
<%
CurQ = Request.Form("CurQ")
Answ = Request.Form("Answ")
correct=Request.Form("Correct")
wrong=Request.Form("Wrong")
This first part puts the values from the forms (if any) into variables. Saves
on typing. Also, include the PoorManIsNull routine between these two areas of
code
If PoorMansIsNull(CurQ) Then
'If they are just loading the page for
'the first time,
'set the current question number
CurQ = 1
correct = 0
wrong = 0
End If
If PoorMansIsNUll(Answ) Then
If they don't have an answer, then they are coming from the response page
(that tells them if they got their previous question right) If this is the
case, move them to the next question and display it.
CurQ = CurQ + 1
If CurQ > (Your maximum number of questions) Then %>
Congratulations. You have completed this test. You missed <%=wrong%>
questions,
but got <%=correct%> questions right. That is equivilent to a
<%=(correct/(max#ofQs)%>%.
Thank you for doing the test.
If they have completed the test, let them know, show them their score. and be
done with it.
<%
End If
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
%>
This code opens the database and moves the record to the current question. If
you didn't know that, then you should take a web database review either on
4GuysFromRolla.com or
http://www.activeserverpages.com/learn
<h2>Question Number <%=rsTemp("QuestionID")%> </h2>
<form method=POST action="myASP.ASP">
<input type=hidden name=CurQ value=<%=CurQ%>>
Your question is <%=rsTemp("Question")%>
Display their question.
I'm not sure about this code below. Essentially, you want to create a drop
down box where the first option is <%=rsTemp("AnswerA")%> and value=1,
and second is <%=rsTemp("AnswerB")%> and value=2. I'm not the best on forms,
so I tried:
Answer: <select name="AnsW">
<option value=1><%=rsTemp("AnswerA")</option>
<option value=2><%=rsTemp("AnswerB")</option>
<option value=3><%=rsTemp("AnswerC")</option>
<option value=4><%=rsTemp("AnswerD")</option>
</select>
That is the code for the drop down box. I realize that you may want to use
radioboxes in some cases. If this is the case, you would need to add a handler
for AnsW, where it is set to something like this:
If Request.Form("Radio1") Then ansW = 1
If Request.Form("Radio2") Then ansW = 2
If Request.Form("Radio3") Then ansW = 3
If Request.Form("Radio4") Then ansW = 4
That is just a rough idea.
<input type=hidden value="<%=correct%>">
<input type=hidden value="<%=wrong%>">
<input type=reset value="Clear the Form"><input type=submit value="OK!">
</form>
This code just closes off the form:
<%
Else
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
If AnsW isn't empty, then it must contain something, using logic. Therefore,
the logical step would be to compare this Answer with the actual Answer
If AnsW = rsTemp("CorrectAns") Then
'They got it right
%>
<p>Congratulations. You got it right. Whee</p>
<% correct = correct + 1 %>
<% Else
'They missed the question %>
<p>I'm sorry, you missed the question. You can review by reading: </p>
<p><%=rsTemp("reference")</p>
<% wrong = wrong + 1 %>
<% End If %>
This closes out writing the responses, but we still aren't done yet. we now
add a button for the advancement of the questions. This appears on the
congratulations
and the sorry page because we already closed the If/Then checking whether they
were right.
<form method=POST action="myASP.ASP">
<input type="hidden" name=curQ value="<%=curQ%>">
<input type="hidden" name=correct value="<%=correct%>">
<input type="hidden" name=wrong value="<%=wrong%>">
<input type="submit" value="Next Question"%>
</form>
<% End If %>
Wow. That is it. All of the code is right there. Here, let me paste EVERY single
bit of the code for you as well. (Click here to view just the code.)
<%
CurQ = Request.Form("CurQ")
Answ = Request.Form("Answ")
correct=Request.Form("Correct")
wrong=Request.Form("Wrong")
'Poor Man's IsNull Code goes here
If PoorMansIsNull(CurQ) Then
CurQ = 1
correct = 0
wrong = 0
End If
If PoorMansIsNUll(Answ) Then
CurQ = CurQ + 1
If CurQ > (Your maximum number of questions) Then
%>
<p>Congratulations. You have completed this test. You missed <%=wrong%>
questions,
but got <%=correct%> questions right. That is equivilent to a
<%=(correct/(max#ofQs)%>%.
Thank you for doing the test.
<% End If %>
<%
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
%>
<h2>Question Number <%=rsTemp("QuestionID")%> </h2>
<form method=POST action="myASP.ASP">
<input type=hidden name=CurQ value=<%=CurQ%>>
Your question is <%=rsTemp("Question")%><br>
Answer:
<select name="AnsW">
<option value=1><%=rsTemp("AnswerA")</option>
<option value=2><%=rsTemp("AnswerB")</option>
<option value=3><%=rsTemp("AnswerC")</option>
<option value=4><%=rsTemp("AnswerD")</option>
</select>
<input type=correct value="<%=correct%>"><input type=wrong value="<%=wrong%>">
<input type=reset value="Clear the Form"><input type=submit value="OK!">
</form>
<% Else %>
<%
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
If AnsW = rsTemp("CorrectAns") Then
%>
<p>Congratulations. You got it right. Whee</p>
<% correct = correct + 1 %>
<% Else %>
<p>I'm sorry, you missed the question. You can review by
reading: </p>
<p><%=rsTemp("reference")</p>
<% wrong = wrong + 1 %>
<% End If %>
<form method=POST action="myASP.ASP">
<input type="hidden" name=curQ value="<%=curQ%>">
<input type="hidden" name=correct value="<%=correct%>">
<input type="hidden" name=wrong value="<%=wrong%>">
<input type="submit" value="Next Question"%>
</form>
<% End If %>
Happy Programming!
Attachments:
Article Code - Text file format
This article was written by
Ryan S.
Ryan has been a computer programmer
in the loosest sense since the age of 8. He has been working with ASP
since the age of 13, when it first came out (that he knows of), and is
somewhat advanced at it.