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!

Here is the full quiz I made

And its source code

8 thoughts on “Simple PHP quiz

  1. sell gold coins May 4, 2011 at 8:09 am

    Hi, I just read texts on your blog and I became interested in the topic. I like your site and I am thinking whether I could use your words in my work? Would it be possible? If yes, please contact with me.Thanks.

  2. sell gold for cash May 5, 2011 at 12:28 pm

    I’m glad I found this web site, I couldn’t find any knowledge on this matter prior to. Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site.

  3. Ambit Energy May 7, 2011 at 6:04 pm

    Thanks for the?interesting post.? I’m going to share with my friends and look forward to visiting this site again!

  4. Stitches May 9, 2011 at 6:48 am

    TYVM you’ve solved all my probmles

  5. cheryl May 17, 2014 at 2:01 pm

    Hi there
    I need to make quiz and have it email the results to a person how do i do this on a localhost home webserver?

    Thanks for your help

    • pyrocam May 18, 2014 at 8:39 pm

      Hi, check out php.net/manual/en/function.mail.php
      you would need to ensure your home webserver has its configuration set to use a mail server.

  6. maow July 31, 2014 at 7:21 am

    Hey hello !
    Okay so i need your help in something, if you can
    I have kind of the same quiz you have, which is nice, but the thing is, i would like to compel (yeah, i know it’s a strong word but my english needs more coffee to come back to my mind) the guy to answer each and every question before submitting. So i want to find how to check that before the form’s action… goes in action.
    Thanks i you can help ! and congrats if you understand haha

  7. pyrocam July 31, 2014 at 8:58 pm

    maow :

    Hey hello !
    Okay so i need your help in something, if you can
    I have kind of the same quiz you have, which is nice, but the thing is, i would like to compel (yeah, i know it’s a strong word but my english needs more coffee to come back to my mind) the guy to answer each and every question before submitting. So i want to find how to check that before the form’s action… goes in action.
    Thanks i you can help ! and congrats if you understand haha

    Hi Maow. yes this is possible,
    the easiest might be to check the content of the post data, and if its empty then perform an action, ie, print “this question requires data”
    w3schools.com/php/php_form_required.asp should help better than I can 🙂

Leave a Reply

Name *
Email *
Website

Protected by WP Anti Spam