• home
  • forum
  • my
  • kt
  • download
  • Creating a Simple Quiz Script

    Author: 2008-08-24 10:12:57 From:

    First of all, you're gonna need questions:

    CODE
    $form_block = "
    <p>Quiz</p>
    <form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
    <p><strong>What's The Capital City Of England?</strong><br>
    <input type=\"text\" name=\"q1\" value=\"$q1\" size=30></p>
    <p><strong>How Many Letters Are There In The Alphabet?</strong><br>
    <input type=\"text\" name=\"q2\" value=\"$q2\" size=30></p>
    <p><strong>WHat's 3+10?</strong><br>
    <input type=\"text\" name=\"q3\" value=\"$q3\" size=30></p>
    <input type=\"hidden\" name=\"execute\" value=\"1\">
    <p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p>
    </form>";


    That takes the form of a form...
    You can, of course edit the questions.
    I will now break that code down and explain the different parts.

    CODE
    <form method="POST\" action=\"$_SERVER[PHP_SELF]\">


    The reason that any "s (Double quotes) are shown after a \ (Backwards slash) is becouse the PHP Parser will spew out errors if you forget to do this.
    "POST" Means that it will carry all the data from this form via POST, rather than GET.
    "$_SERVER[PHP_SELF]" Means that it will load this page again to process the data from the form.

    CODE
    <input type="hidden\" name=\"execute\" value=\"1\">


    This will tell the PHP document weather to display the form or process the results.

    Then, you're gonna need to display the form

    CODE
    if ($execute != 1) {
    echo "$form_block";
    }


    CODE
    if ($execute != 1){


    Will find out if "$execute" is not equal to 1. If not, it will display the form like so:

    CODE
    echo "$form_block";



    Submit:

    CODE
    else if ($op == "ds") {


    That'll do the opposite, it "$execute" IS equal to 1, it will process the answers.

    Make all the answers in lower case:

    CODE
    strtolower($q1);
    strtolower($q2);
    strtolower($q3);




    And the answers, you're gonna need answers

    CODE
         if ($q1 == "london") {
                echo "Question 1 corect!<br/>";
                $score = "1";
             } else {
                echo "Try question one again!<br/>";
             }
             if ($q2 == "26") {
                echo "Question 2 correct!<br/>";
                $score = $score + 1;
             } else {
             echo "Try question 2 again!<br/>";
             }
             if ($q3 == "13") {
                echo "Question 3 correct!<br/>";
                $score = $score + 1;
             } else {
                echo "Try question 3 again!<br/>";
             }
    }


    You should be able to understand that, from what i've told you in the rest of the tut.

    So the whole code is this:

    CODE
    <?php
    //Make the form
    $form_block = "
    <p>Quiz</p>
    <form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
    <p><strong>What's The Capital City Of England?</strong><br>
    <input type=\"text\" name=\"q1\" value=\"$q1\" size=30></p>
    <p><strong>How Many Letters Are There In The Alphabet?</strong><br>
    <input type=\"text\" name=\"q2\" value=\"$q2\" size=30></p>
    <p><strong>WHat's 3+10?</strong><br>
    <input type=\"text\" name=\"q3\" value=\"$q3\" size=30></p>
    <input type=\"hidden\" name=\"execute\" value=\"1\">
    <p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p>
    </form>";

    //Display the form.
    if ($execute != 1) {
    echo "$form_block";
    //OR
    }else if ($op == "ds") {
    //Process and display the results.

    //Make all the user input Lower Case.
    strtolower($q1);
    strtolower($q2);
    strtolower($q3);

    //Display results.
         if ($q1 == "london") {
                echo "Question 1 corect!<br/>";
                $score = "1";
             } else {
                echo "Try question one again!<br/>";
             }
             if ($q2 == "26") {
                echo "Question 2 correct!<br/>";
                $score = $score + 1;
             } else {
             echo "Try question 2 again!<br/>";
             }
             if ($q3 == "13") {
                echo "Question 3 correct!<br/>";
                $score = $score + 1;
             } else {
                echo "Try question 3 again!<br/>";
             }
    }
    ?>




    You can of course, change the questions and answers, add questions and stuff.

    When i get time, i'll write a tutorial to make a PHP Quiz with MySQL Questions and Answers.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (12)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (24)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (54)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot