Siby Xavier
Siby Xavier

Reputation: 136

how to select multiple radio button in same while loop which is echoed from db

guyz, i want to select radio button for each question.please help me to do that.now i can select only one radio button in a page...... check this link: https://i.sstatic.net/UI2vp.png

while($row= mysql_fetch_row($rs)){

echo "<form name=myfm method=post action=Quiz.php>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
$n=$n+1;
echo "<tr><td><span class=style2>Question  ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name='ques[$n][]' value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name='ques[$n][]' value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name='ques[$n][]'  value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name='ques[$n][]'  value=4>$row[6]";

}

How to fetch the data and store it to mydb...

    $query="select * from question";

    $rs=mysql_query("select * from question where testid=$tid",$cn) or die(mysql_error());

             if($submit=='Get Result')
            {
                $n=0;
                    while($row= mysql_fetch_row($rs)){

                        for($i=0;$i<count($_POST['ques']);$i++)
        {
            $ans=$_POST['ques'][$n][$i];
            echo $ans;
            $n=$n+1;
                            mysql_query("insert into useranswer(sessid, testid, ques, ans1,ans2,ans3,ans4,correctans,yourans) values ('".session_id()."',       $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());}
                    }
  }

is this works???....

Upvotes: 2

Views: 1724

Answers (3)

Thanga
Thanga

Reputation: 8111

The mistake is that you are naming all your radio buttons with the same name. That makes all of them in one group. Name each group with three different names. Your problem is solved

Upvotes: 0

Franz Gleichmann
Franz Gleichmann

Reputation: 3579

You have to name your radiobuttons for each set.

while($row=mysql_fetch_row($rs)) {
  echo "<form name=myfm method=post action=Quiz.php>";
  echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
  $n=$n+1;
  echo "<tr><td><span class=style2>Question  ".  $n .": $row[2]</style>";
  echo "<tr><td class=style8><input type=radio name='ques".$n."' value=1>$row[3]";
  echo "<tr><td class=style8> <input type=radio name='ques".$n."' value=2>$row[4]";
  echo "<tr><td class=style8><input type=radio name='ques".$n."'  value=3>$row[5]";
  echo "<tr><td class=style8><input type=radio name='ques".$n."'  value=4>$row[6]";

  echo "</table></table></form>"; //AND you should close your tags
}

Also, you should refrain from using mysql_functions, which are deprecated and removed in PHP7.

Upvotes: 0

Amit Rajput
Amit Rajput

Reputation: 2059

Just replace ques[] with ques[". $n ."][] like this..

while($row= mysql_fetch_row($rs)){

echo "<form name=myfm method=post action=Quiz.php>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
$n=$n+1;
echo "<tr><td><span class=style2>Question  ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name='ques[".$n."][]' value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name='ques[". $n ."][]' value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name='ques[". $n ."][]'  value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name='ques[". $n ."][]'  value=4>$row[6]";

}

Upvotes: 1

Related Questions