|
|
|
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 ---
|

|
|
 |
php, shoutbox problems Author: vanhansen Posted: Nov 17th, 1:30am Activity: 5 replies, 106 views
|  | MarkupGeeks Logo Author: ahstanford Posted: Nov 16th, 8:45pm Activity: 13 replies, 197 views
|  | Drawing Tutorials Author: ahstanford Posted: Nov 16th, 12:46am Activity: 0 replies, 119 views
|  | Superbowl predictions, anyone? Author: ahstanford Posted: Nov 15th, 10:46pm Activity: 10 replies, 168 views
|  | Photomanipulation Footsteps Author: ahstanford Posted: Nov 15th, 10:43pm Activity: 4 replies, 118 views
|  | Learning to draw... Author: ahstanford Posted: Nov 15th, 12:43pm Activity: 4 replies, 135 views
|  | Looking for simple UI elements Author: FenixRoA Posted: Nov 15th, 6:40am Activity: 7 replies, 125 views
|  | HDD Help? Author: Phoenix Wynde Posted: Nov 13th, 2:31am Activity: 1 replies, 125 views
|  | Fun New Battles Posted! Author: ahstanford Posted: Nov 11th, 7:33pm Activity: 0 replies, 155 views
|  | 4-man Simon Tournament Author: ahstanford Posted: Nov 11th, 3:28pm Activity: 0 replies, 102 views
|  | Design Brief Inspiration for BioRUST Battles! Author: ahstanford Posted: Nov 11th, 7:19am Activity: 4 replies, 153 views
|  | The BioRUST Free Stock Photography Thread Author: ahstanford Posted: Nov 11th, 6:32am Activity: 2 replies, 173 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|