Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

A Simple Quiz Script


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!

- Tutorial written by Scrowler

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  mikeys4u (#57110)
Date: Tue Jun 23, 2009. 21:23:14

Post #1 of 1

I really like the coding for this quiz its simple.... I am creating some code to choose a quiz name and add to a text file questions you choose, by appending to a text file.

Task: -
I want to use this coding but in ONE file, not two.....

Problems I have encountered: -
1. Cant add the contents of Quiz.asp to data...(which would be ideal)
2. Include files cant contain a variable....which would help as I could make it dynamic.....
3. Coffee keeps going cold.....

Please help.......

Reply to this post


--- View Entire Thread ---
Amazing Font Pack!

Featured Tutorialsmore

Extracting Hair
Extracting Hair
- Adobe Photoshop -
Hershey Kisses
Hershey Kisses
- Adobe Illustrator -
Metal Wires & Cables
Metal Wires & Cables
- Adobe Photoshop -
Grunge Rubber Stamp
Grunge Rubber Stamp
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Download Source File
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673