Simple PHP quiz
Make your own online quiz with PHP easily
Disclaimer: All care No Responsibility. you follow the instructions provided at your own risk. I take no responsibility any damage you may cause. Please read the article in full before starting
I thought it would be fun to make a little quiz the other day and it was pretty simple, so I thought I would share it.
Basically, using post, if / else and some radio buttons you can make a simple quiz that looks good and is pretty easy to create and update.
the quiz runs in a single page, so first thing I do is to see if the quiz has already been taken by checking a post variable specifically for this purpose.
?php
if($_POST['done'] != 1 )
{?;
so now I know its not a completed quiz, so I start asking questions. for the purpose of this I am going to only use 2 questions.
I am going to open up a form, posting to the same page and then use radio buttons to set individual variables which I have called opvar2 and opvar1
form action="quiz.php" method="post" /;
Do you use a terminal?
input type="radio" name="opvar1" value="1"; Yes
input type="radio" name="opvar1" value="0"; No
how many domains do you manage?
input type="radio" name="opvar2" value="0"; I don't
input type="radio" name="opvar2" value="1"; 1
input type="radio" name="opvar2" value="2"; 2
input type="radio" name="opvar2" value="3"; 3
input type="radio" name="opvar2" value="4"; 4
input type="radio" name="opvar2" value="5"; 5 or more
OK so we have two variables opvar2 and opvar1 with corresponding values eg, ’2′ and ’4′ we need to close this form off and give it a submit button but we add in one more variable so we can identify whether or not this page has been submitted or not.
input type="hidden" name="done" value="1"/;
input type="submit" value="Submit...."/;
/form;
All the questions are primed and ready to go, pressing the submit button will refresh the page posting the results to itself. So now we need to look at that and do something with it!
?php
} else {
$lopvar2=$_POST['opvar2'];
$lopvar1=$_POST['opvar1'];
$total = $lopvar2 + $lopvar1;
print $total;
}
?;
So if the page is returning stuff in post, we go straight down to this last ELSE statement, pickup the variables and add them, for now it just posts them plainly but later I will demonstrate how to use a ‘if / else’ statement to get some nicer results!
- No comments yet.
