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

|
|
 |
[WIP] Hairdresser Site Design Author: YinToniq Posted: Jul 03rd, 5:58am Activity: 1 replies, 28 views
|  | Simple Flash Help Needed (AS2) Author: stiney51 Posted: Jul 02nd, 2:40pm Activity: 9 replies, 53 views
|  | Transformers Author: Jacorre Posted: Jun 29th, 6:33pm Activity: 2 replies, 72 views
|  | Quick question about Jasc PaintShop Pro 9. Author: Rydium-41 Posted: Jun 28th, 8:21pm Activity: 2 replies, 72 views
|  | I bought film today!! Author: NikonErik Posted: Jun 27th, 12:04am Activity: 2 replies, 74 views
|  | New Battle Author: MoodsR4Cattle Posted: Jun 26th, 6:28pm Activity: 2 replies, 82 views
|  | Image Loading Author: funkyfela Posted: Jun 22nd, 9:00pm Activity: 1 replies, 83 views
|  | Headshot Critique Please Author: NikonErik Posted: Jun 19th, 11:32pm Activity: 6 replies, 152 views
|  | imagecopy() and imagecopyresized() problem Author: thatpyrokid Posted: Jun 18th, 3:05pm Activity: 1 replies, 147 views
|  | Problem With Regitration Code...? Author: RXS Posted: Jun 17th, 3:18am Activity: 2 replies, 147 views
|  | Topaz Labs do it again! Author: Tamlin Posted: Jun 17th, 12:08am Activity: 5 replies, 227 views
|  | [Photoshop] Anime Coloring Tutorial Author: Wavechan Posted: Jun 16th, 3:47pm Activity: 2 replies, 290 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|