|
|
|
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>• " & 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>• " & 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
| 
|
|
|
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 ---
|

|
|
 |
TrentonCS Author: ahstanford Posted: Nov 06th, 7:40pm Activity: 3 replies, 28 views
|  | Html 5 Author: ahstanford Posted: Nov 05th, 1:32pm Activity: 5 replies, 90 views
|  | What are your favorite websites? Author: ahstanford Posted: Nov 05th, 12:51am Activity: 0 replies, 47 views
|  | What do you do for a living? Author: ahstanford Posted: Nov 04th, 11:04pm Activity: 0 replies, 46 views
|  | What is your favorite Subway sandwich? Author: ahstanford Posted: Nov 04th, 11:02pm Activity: 4 replies, 90 views
|  | Windows 7 Author: ahstanford Posted: Nov 04th, 10:59pm Activity: 0 replies, 50 views
|  | Google Wave Author: ahstanford Posted: Nov 04th, 10:52pm Activity: 0 replies, 60 views
|  | University Project Author: Gjbphp Posted: Nov 03rd, 8:59pm Activity: 1 replies, 91 views
|  | Hello BioRust! Author: ahstanford Posted: Nov 02nd, 5:39pm Activity: 4 replies, 88 views
|  | Illustrator cs4 - Convert outlines/graphics to ... Author: izidrew Posted: Oct 29th, 3:48pm Activity: 3 replies, 264 views
|  | Hello BioRust!! Author: MOST Posted: Oct 29th, 12:52am Activity: 4 replies, 144 views
|  | Hey! newbie Author: prelimiting Posted: Oct 28th, 11:51pm Activity: 1 replies, 114 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|