Downloaded from www.biorust.com on Thu Feb 09, 2012 11:20:16

 
A Simple Quiz Script
Tutorial Author - Scrowler (http://forums.biorust.com/member.php?userid=66)

ASP is a very simple language if you know its syntax (in that way, it is much like every other programming language, albeit in ASP's case a bit more forgiving than some others).  This tutorial describes the construction of a very simple ASP quiz script, which works with just two files: quiz.asp and data.asp. Let’s start by looking at quiz.asp:

Quiz File (quiz.asp)

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="data.asp"-->
<%
if request.form("ProcessQuiz") = "" then
call ShowQuiz()
else
call ProcessQuiz()
end if
%>

Yep, that’s it! So, what did we do? Well, we did three things:
- Told the server that we are using VBscript.
- INCLUDEd the data.asp file.
- Specified an IF switcher, which gives the script different outputs, depending on whether the form has been submitted or this is the first time the script has been run by the user.

With that out of the way, let’s break down data.asp:

Data File (data.asp) 

<%
dim Quiz(5) ' change number to num of questions you want to show

Quiz(0) = "How old is scrowler?|14|10|12|14|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|Above 30"

Quiz(1) = "What is scrowler's first name?|Robbie|John|James|Charles|Robbie|Sam|Lewis"

Quiz(2) = "What did scrowler call his first ASP layout?|ASP scrowler|ASP|scrowler's ASP|ASP's scrowler|ASP scrowler|Dunno"

Quiz(3) = "What is scrowler's favourite programming language?|PHP|ASP|PHP|Perl|Python|HTML"

Quiz(4) = "What is scrowler's last name?|Averill|Smith|Dweezle|Moonunit|Johnson|Averill"

This little snippet of code is very simplem and defines the array 'Quiz' with 5 entries, which can be modified easily to include more or fewer questions. Then it defines each one, like this:

Quiz(Question number – 1) = “Question|CorrectAnswer|Answer1|Answer2|CorrectAnswer|Answer4”

So basically, Quiz with the key number – 1 is equal to a text string, which contains the question, the correct answer identifier, and then the rest of the answers, including the correct one.

Next up, our first function:

public Sub ShowQuiz()

response.write("<form action='quiz.asp' method='post'>")

for i = 0 to ubound(Quiz) - 1
   Bits = split(Quiz(i) , "|")
   response.write("<p>&bull; " & Bits(0) & "<br>")
   response.write("<select name='question" & i + 1 & "' class='tb'>")

for x = 2 to ubound(Bits)
   response.write("<option value='" & Bits(x) & "'>" & Bits(x) & "</option>")
   next

response.write("</select></p>")
next
response.write("<p><input type='submit' name='ProcessQuiz' value='Check answers' class='tb'></p></form>")

End sub

This function cycles through each array stream (each one is a question/answer set) and splits the text up into the Question, the Correct Answer Identifier, and then the list of possible answers. It then creates a selectbox, outputs all the answers as options into the selectbox, and then closes it properly.

Our second and final function checks the answer you submitted:

public sub ProcessQuiz()

for i = 0 to ubound(Quiz) - 1
  Bits = split(Quiz(i) , "|")
  if Bits(1) = request.form("Question"&i+1) then
     Result = "<font color='#009900'>Correct</font>"
  else
     Result = "<font color='#ff0000'>Incorrect</font> - The correct answer was " & Bits(1)
  end if

response.write("<p>&bull; " & Bits(0) & "<br>")
response.write("You answered: " & request.form("Question"&i+1) & ". This was " & Result)
response.write("</p>")
next

end sub
%>

This function cycles through the streams again, splits up the data, checks the submitted answer against the Correct Answer Identifier in the string, then outputs the result accordingly.

And that’s it! If you want help, contact me on the forums and I will help out!  Until next time... have fun coding!


All Content © BioRUST 2012 All Rights Reserved.