mysql - How to validate a selected radio button in a quiz system using php -


i preparing quiz app using php , mysql. having problem validating radio button.

when click on check button checks whether first option correct or not should check selected option in radio button correct or not.

the code have used here:

$singlesql = mysql_query("select * questions id='$question' limit 1"); while($row = mysql_fetch_array($singlesql)){     $id = $row['id'];     $thisquestion = $row['question'];     $type = $row['type'];     $subject =$row['subject'];     $exam =$row['exam'];     $explan =$row['explan'];     $question_id = $row['question_id'];     $s ='<strong>'.$subject.'</strong>';     $e ='<small>'.$exam.'</small>';     $q = '<h2>'.$thisquestion.'</h2>';     $ex ='<div id="welcomediv" style="display:none;" class="expl" >'.$explan.'</div>';     $sql2 = mysql_query("select * answers question_id='$question' order rand()")or die(mysql_error());      while($row2 = mysql_fetch_array($sql2)){         $id2=$row2['id'];         $answer = $row2['answer'];         $correct = $row2['correct'];               $answers .= '<table class="table table-hover table-bordered">                      <tr>                     <td class="chk">                     <label style="cursor:pointer;">                     <input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label></td>             </tr>                 </table>         <input type="hidden" id="qid" value="'.$id.'" name="qid"><br />';      $result=mysql_query("select id answers question_id='$question' ");     $nrows=mysql_num_rows($result);     for($i=0;$i<=4;$i++){         if (isset($_post[$correct])) {             $answer= $_post[$correct];         }          if($answer&&$correct==1){             echo $dv.='<div style="display:none;" class="green" id="chek" >your answer '.$answer.'  correct</div>';         } else {             echo $dv2.='<div style="display:none;" class="red" id="chek" >your answer '.$answer.'  worng</div>';}         } 

i have 2 tables 1 questions , anothher answers

questions table

            id question_id question               1  1          question1 

answers table

            id questions_id answer correct             1       1        op1    1             2       1        op2    0             3        1       op3    0             4        1       op4    0 

name="rads" problem should have unique name every group of answers.

you should add this:

$i = 1;  while($row2 = mysql_fetch_array($sql2)){         $id2=$row2['id'];         $answer = $row2['answer'];         $correct = $row2['correct'];               $answers .= '<table class="table table-hover table-bordered">                      <tr>                     <td class="chk">                     <label style="cursor:pointer;">                     <input type="radio" name="rads_'.$i.'" value="'.$correct.'">'.$answer.'</label></td>             </tr>                 </table>         <input type="hidden" id="qid" value="'.$id.'" name="qid"><br />'; $i++; 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -