I have a quiz test page where I want to test the user for knowledge. I did it so it works, but it has a flaw. This is my code for populating the quiz with questions and (possible) answers from a mysql database:
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="post" id="quiz">';
$sql=mysql_query("SELECT idtest,no_q,title_q,q,idteste_dtl,corect_a from teste_dtl where idtest=".$idtest." order by no_q");
$num = mysql_num_rows($sql);
$i=0;
while ($row=mysql_fetch_row($sql)) {
echo '<h3 style="color:#e74c3c">'.$row[2].'</h3>';
echo '<p data-toggle="tooltip"><strong>'.$row[3].'</strong></p>';
$ssql=mysql_query("select letter,answer from tests_answers where idtest=".$idtest." and idq=".$row[4]." order by letter");
while ($rrow=mysql_fetch_row($ssql)) {
$Label = 'question-'.$row[1].'-answers-'.$rrow[0];
echo '<div>';
echo '<label class="radio">';
echo '<input type="radio" data-toggle="radio" name="answers['.$row[1].']" id="'.$Label.'" value="'.$rrow[0].'" unchecked>';
echo $rrow[0].') '.$rrow[1];
echo '</label>';
echo '</div>';
}
echo ' <br>';
echo '<input type="hidden" name="question['.$row[1].']" value="'.$row[2].'">';
echo '<input type="hidden" name="corect_a['.$row[1].']" value="'.$row[5].'">';
echo '<input type="hidden" name="idemp" value="'.$idemp.'">';
echo '<input type="hidden" name="idtest" value="'.$idtest.'">';
echo '<input type="hidden" name="idfirm" value="'.$idfirm.'">';
echo '<input type="hidden" name="namefirm" value="'.$namefirm.'">';
echo '<input type="hidden" name="totalq" value="'.$num.'">';
}
echo' <input type="submit" class="btn btn-hg btn-primary" value="Check answers" />';
echo '</form>';
So basically I generate a pair of text (question) + radioboxes (possible answers) from 2 tables. Table 1 is called "teste_dtl" and Table 2 is called "tests_answers". All works perfectly except there is a bug. If the user does not check a radio box in a question... my code seems to think that the first radio box was checked. So I have something like:
Question ONE
Body of question one... some text... and so on
A) first answer
B) second answer
C) third answer
D) fourth answer
Question TWO
Body of question two... some text... and so on
A) first answer
B) second answer
C) third answer
D) fourth answer
...
So the A,B,C,D... are checkboxes (input type radio). They are all unchecked from the start. When I POST the results... apparently the questions that were not answered (no radio was checked) are considered as "answered with answer A)"
How can I avoid this? Thank you