Jump to content

How to validate an answer in radio button using php


naveen212010

Recommended Posts

when i am selecting a answer in a radio button of four options it only compares first option of all the options ,

the will have to see which answer is selected and verify that selected answer is correct or not and display corect if correct and display wrong if wrong 

example go to this link http://www.naveenr5.5gbfree.com/quiz.php?question=4

 

There are two tables named questions and answers questions table is 

 

questions table is 

            

                          id question_id question 

              1    1        question1
              2    2        question2

answers table is 

id question_id answer  correct

1      1       answer1  0
2      1       answer2  1
3      1       answer3  0
4      1       answer4  0

same for  second  question 

 

the php code iam using is here 

 

       

 

session_start();

require_once("scripts/connect_db.php");
$arrCount = "";
if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$output = "";
$answers = "";
$q = "";
$dv="";
$dv2="";
 
 
$singleSQL = mysql_query("SELECT * FROM questions WHERE 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 * FROM answers WHERE question_id='$question' ORDER BY rand()");
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 FROM answers WHERE 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.'  is correct</div>';}
 
   else{
echo  $dv2.='<div style="display:none;" class="red" id="chek" >Your answer '.$answer.'  is worng</div>';}
 }
 
 
}
$output = ''.$s.','.$e.''.$q.','.$dv.''.$dv2.''.$answers.''.$ex.'<input type="button" name="answer" value="check" onclick="showDiv();chk();"/> <span ><button onclick="post_answer()" class="btn" >Submit</button></span>';
echo $output;
  }
}
 
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.