0

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

4

1 回答 1

1

Radio buttons are supposed to always have a value.

The HTML specification says: "If no radio button in a set sharing the same control name is initially 'on', user agent behavior for choosing which control is initially 'on' is undefined." This means, practically, that depending on the browser and/or the Javascript library used to submit the form (if you're using AJAX), you can get unpredictable results.

You should force one of the buttons to be selected either by inserting checked="checked" like this:

$first_answer = true; // Set a flag for the first answer
while ($rrow=mysql_fetch_row($ssql)) {
   $Label = 'question-'.$row[1].'-answers-'.$rrow[0];
   echo '<div>';
   echo '<label class="radio">';
   // Output the beginning of the INPUT tag
   echo '<input type="radio" data-toggle="radio" name="answers['.$row[1].']" id="'.$Label.'" value="'.$rrow[0].'"';
   if ( $first_answer ) { // When the flag is true...
       echo ' checked="checked"'; // ...add the 'checked' parameter...
       $first_answer = false; // ...and set the flag to false for the next round
   }
   echo '/>'; // Close the INPUT tag
   echo $rrow[0].') '.$rrow[1];
   echo '</label>';
   echo '</div>';
}

Another option is to add some Javascript to check that the user has selected an answer.

于 2013-11-11T02:32:43.473 回答