Question

I'm trying to make a quiz form which displays questions and 3 answers which are taken from my database. When I run my loop, it displays all the questions and answer choices correctly with the radio button but when I click on question 1 answer "b" then click question 2 answer "c" the radio button un-clicks question 1's answer "b" and highlight question 2 answer "c";

Answering question 1             Trying to answer question 2 after answering question 1
1. What is your name?                  1. What is you name?
   ()John                                  ()John
   (*)Jake                                 ()Jake <--un-clicked
   ()Joe                                   ()Joe
2. Where are you from?                 2. Where are you from?
   ()San Antonio                           ()San Antonio   
   ()Austin                                ()Austin
   ()New York                              (*)New York

If you notice question 1 answer is removed when question 2 is answered. Here is my code that retrieves the data from the database and displays it.

    $mysql = "SELECT * FROM $table WHERE $table.quiz_name = '$name'";

    $mydata = mysql_query($mysql,$con);
    //post quiz name (here)
    echo $name."</br>";
    while($records = mysql_fetch_array($mydata)){

    echo "<div>";
         echo $records['question_description']."<br>";

        //image displayed here  

        echo "<label><input type='radio' name='option' value=".$records['option_a'].">".$records['option_a']."</label><br>";
         echo "<label><input type='radio' name='option' value=".$records['option_b'].">".$records['option_b']."</lable><br>";
         echo "<label><input type='radio' name='option' value=".$records['option_c'].">".$records['option_c']."</label><br>";

         echo "</ br> <hr>";
    echo "</div>";
     }

I hope someone can help me solve this issue.

Was it helpful?

Solution

you are grouping all radio buttons with 'option'... do this;

$counter = 0 //declare a counter outside the loop

and add this with the name

 name='option".$counter."'

and increment counter inside the loop

$counter++

This will give you a group for each set of radio buttons

OTHER TIPS

Your radio buttons need different names. Having them all with the same name will mean you can only select one out of every radio you have.

Your radio buttons should have a different name for each question. Consider using something like your database ID like so:

while($records = mysql_fetch_array($mydata)) {
    echo $records['question_description']."<br>";

    echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_a'].">".$records['option_a']."</label><br>";
    echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_b'].">".$records['option_b']."</lable><br>";
    echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_c'].">".$records['option_c']."</label><br>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top