Bollywood India forum movie reviews - Yellowline - Lab Equipment - Online Name - Find jobs - Loans

PDA

View Full Version : creating a multiple choice quiz


sempastian
Mar 25th 2009, 9:19 am
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>

i only put two questions so far but i would like some help on what to do next.
thank you

shallowink
Mar 25th 2009, 9:29 am
might have easier to do it like this:
<p>
<input type="checkbox" name="1700" />
1700</p>

This page shows how to process the values etc.
http://www.homeandlearn.co.uk/php/php4p11.html

skyfe
Mar 25th 2009, 10:14 am
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

}
?>



Skyfe.

SmallPotatoes
Mar 25th 2009, 4:04 pm
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.

sempastian
Mar 28th 2009, 9:17 pm
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.