i am a newbie in web developing... i'm trying to create an online quiz... i am using mysql and php... i was able to display the questions and choices (multiple choice using radiobuttons) generated from the database... however, i am having a hard time getting the values of the radiobuttons selected so that i can compare them with the right answers then compute for the score... i really need your help guys... anything will be appreciated... i am really lost now... please...
and use checkboxes for multiple choice, otherwise you'll confuse the user!
Hmmm?????
For most multiple-choice questions you're only supposed to select one of the choices, so radio buttons would seem to be the correct implementation over checkboxes.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
You wouldnt like to post some code would you? use sessions when you set hte answers
forgive me for my stupidity... but can you please explain that further... i've been trying to use this code... but i really don't know where to go from here.. i was able to get the expected outcome, if and only if, i only have one record on the database... but as soon as i add more records on it... it only displays the value of the last radiobutton i clicked... any help will still be appreciated...
For most multiple-choice questions you're only supposed to select one of the choices, so radio buttons would seem to be the correct implementation over checkboxes.
oh yeah sorry, i thought you meant multiple selection, sorry!
i thought of something... however, i am not 100% sure about it... i tried using a for() statement for the name of my radiobuttons... so that each set of radiobuttons will have a distinct name... but my problem now is... how do i use $_POST to get the clicked radiobutton's value... here is the sample code...
If you want a set of radio buttons to be mutually exclusive, i.e. only one of them can be selected, then they need to each have the same name. When you access that name in the $_POST array, you'll get the value of the one which was selected.
I would set up the form like this, using the question ID as the name for each set of radio buttons, so that you can use that to reference back to the DB to check the answer:
PHP Code:
<?php
$connection = mysql_connect("localhost","user","password") or die("Couldn't connect to server.");
$db = mysql_select_db("database",$connection) or die("Couldn't connect to database.");
$sql = "SELECT * FROM table ORDER BY id";
$sql_result = mysql_query($sql,$connection) or die("Couldn't execute query.");
if ($sql_result and mysql_num_rows($sql_result) > 0)
{
echo "<form method='post' action='try2.php'><div>";
while ($row = mysql_fetch_assoc($sql_result))
{
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
echo "<h3>$question</h3>\n";
foreach(array($opt1, $opt2, $opt3, $opt4) as $value)
{
echo "<input type='radio' name='$id' value='$value'>$value <br />";
}
}
echo "<input type='submit' value='Get My Scores' name='submit'>";
echo "</div></form>";
}
?>
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
thanks for the reply... i just have another question... how can i retrieve the value selected using $_POST if i have another variable for its value... i mean... for example, if i wanted to print out the value of the selected radiobutton can i do it this way...
i've actually tried doing those but i just get a blank page... do you think there's something else i need to add since the name of my radiobutton is another variable... do you think i have to do it this way...
thank you so much for the reply... i'll try to work on the code you have posted... but i won't promise that i won't be coming back to ask other questions... you know... in case i get lost again... hehehe... hope you won't be mad at me and would still reply to my posts... i'm pretty sure i still have a lot of things to learn about php...
Bookmarks