creating a multiple choice quiz

Discussion in 'PHP' started by sempastian, Mar 25, 2009.

  1. #1
    hello all.i am making a multiple choice quiz in my website.so far i wrote some code in php i made the form with the questions and the connection to mysql.but am stuck now and i have no idea what to do next.here is the code i wrote so far
    
    <?php
       $connection = mysql_connect("server", "name", "password");
    		// select database
          mysql_select_db("db_db") or die ('Unable to select database!');
    	  ?>
    <form id="form1" name="form1" method="post" action="">
      <p>1. When was the eiffel tower build?</p>
      <p align="left">
        <input type="checkbox" name="checkbox" value="checkbox" />
      1889	</p>
      <p align="left">
        <input type="checkbox" name="checkbox2" value="checkbox" />
      1800</p>
      <p>
        <input type="checkbox" name="checkbox3" value="checkbox" /> 
        1900</p>
      <p>
        <input type="checkbox" name="checkbox4" value="checkbox" />
        1930</p>
      <p>&nbsp;</p>
      <p>2. When was the French Revolution?</p>
      <p>
        <input type="checkbox" name="checkbox5" value="checkbox" />
      1800</p>
      <p>
        <input type="checkbox" name="checkbox6" value="checkbox" />
      1700</p>
      <p>
        <input type="checkbox" name="checkbox7" value="checkbox" />
      1789</p>
      <p>
        <input type="checkbox" name="checkbox8" value="checkbox" />  
       1750</p>
      <p align="center">
        <input type="submit" name="Submit" value="Submit" />
    </p>
    </form>
    </body>
    </html>
    
    PHP:
    i only put two questions so far but i would like some help on what to do next.
    thank you
     
    sempastian, Mar 25, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    shallowink, Mar 25, 2009 IP
  3. skyfe

    skyfe Active Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Next thing you want to do is to check whether the user has chosen an answer for both questions (I assume?), and to check first of all if the submit button has been pressed:

    
    <?php
    
    if(!empty($_POST['submit'])) { //if the submit button has been pressed
    
       if( ($_POST['checkbox'] == "checkbox" ||$_POST['checkbox2'] == "checkbox" || $_POST['checkbox3'] == "checkbox" || $_POST['checkbox4'] == "checkbox") AND ($_POST['checkbox5'] == "checkbox" || $_POST['checkbox6'] == "checkbox" || $_POST['checkbox7'] == "checkbox" || $_POST['checkbox8'] == "checkbox") ) {
    
       //this checks if on both questions has been given an answer to
    
          echo "You answered both questions!";
    
       //now you can check if the answer(s) is(/are) correct by checking if $_POST['checkbox'] == "checkbox" 
       // (replace ['checkbox'] by ['the name of the checkbox containg the correct answer']) to see if the user chosen the right answer
    
    }else{
    
       //show your questions/quiz
    
    }
    ?>
    
    
    Code (markup):
    Skyfe.
     
    skyfe, Mar 25, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Seems like you should be using radio buttons rather than checkboxes, since someone can only choose one answer for each question on a multiple choice quiz.

    But if for some reason you are going to use checkboxes anyway, your life will get 1000x easier if you let them come in as arrays. Instead of:

    checkbox
    checkbox2
    checkbox3
    checkbox4
    -
    checkbox5
    checkbox6
    checkbox7
    checkbox8

    You can just do:

    checkbox[1][]
    checkbox[1][]
    checkbox[1][]
    checkbox[1][]
    -
    checkbox[2][]
    checkbox[2][]
    checkbox[2][]
    checkbox[2][]

    When the form is posted, you'll get a single multidimensional array that contains all of the answers chosen, and you can easily loop through them to score the test.

    Doing it the way you're currently going, it will become more and more of a programming nightmare as your quiz grows, especially if not all questions have the same number of answers.
     
    SmallPotatoes, Mar 25, 2009 IP
  5. sempastian

    sempastian Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thnx for the info guys.i have used radio buttons cause my questions only take one answer. i have made the code and it works fine.thnx again for the help.
     
    sempastian, Mar 28, 2009 IP