Amirtha Rajan Amirtha Rajan - 3 months ago 18
MySQL Question

how to get a value from radio button in php form and check the value in another php

when i select any radio button on this form in index.php ,the condition for the value in radio button is not validated for the correct answer

index.php

<html>
<head>Aptitude Test</head>
<body>
<?php require_once 'config.php';?>
<?php $response=mysql_query("select * from questions");?>
<form method='post' id='quiz_form' >
<?php while($result=mysql_fetch_array($response)){ ?>
<div id="question_<?php echo $result['id'];?>" class='questions'>
<h2 id="question_<?php echo $result['id'];?>">
<?php echo $result['id'].".".$result['question'];?>
</h2>
<div class='align'>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer1_<?php echo $result['id'];?>' for='answer1_<?php echo $result['id'];?>'><?php echo $result['answer1'];?></label>
<br/>
<input type="radio" value="2" id='radio2_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer2_<?php echo $result['id'];?>' for='answer2_<?php echo $result['id'];?>'><?php echo $result['answer2'];?></label>
<br/>
<input type="radio" value="3" id='radio3_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer3_<?php echo $result['id'];?>' for='answer3_<?php echo $result['id'];?>'><?php echo $result['answer3'];?></label>
<br/>
<input type="radio" value="4" id='radio4_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer4_<?php echo $result['id'];?>' for='answer4_<?php echo $result['id'];?>'><?php echo $result['answer4'];?></label>
<input type="radio" checked='checked' value="5" style='display:none' id='radio4_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
</div>
<br/>
<input type="button" id='next<?php echo $result['id'];?>' value='Next!' name='question' class='butt'/>
</div>
<?php }?>
</form>
<div id='result'>
<img src='results.jpg' alt='Results'/>
<br/>
</div>

<div id="demo1" class="demo" style="text-align:center;font-size: 25px;">00:00:00</div>
<script src="jquery-1.9.1.min.js"></script>
<script src="watch.js"></script>
<script>
$(document).ready(function(){
$('#demo1').stopwatch().stopwatch('start');
var steps = $('form').find(".questions");
var count = steps.size();
steps.each(function(i){
hider=i+2;
if (i == 0) {
$("#question_" + hider).hide();
createNextButton(i);
}
else if(count==i+1){
var step=i + 1;
//$("#next"+step).attr('type','submit');
$("#next"+step).on('click',function(){

submit();

});
}
else{
$("#question_" + hider).hide();
createNextButton(i);
}

});
function submit(){
$.ajax({
type: "POST",
url: "ajax.php",
data: $('form').serialize(),
success: function(msg) {
$("#quiz_form,#demo1").addClass("hide");
$('#result').show();
$('#result').append(msg);
}
});

}
function createNextButton(i){
var step = i + 1;
var step1 = i + 2;
$('#next'+step).on('click',function(){
$("#question_" + step).hide();
$("#question_" + step1).show();
});
}
setTimeout(function() {
submit();
}, 50000);
});
</body>
</html>


This is ajax.php which gets the selected radio button answer to check the answer is right or wrong, but the thing is the right answer condition if($_POST[$i] == $row['answer']) fails to check and does not increment the value $right_answer++;

ajax.php

<?php require_once 'config.php';
$query = "select id,question,answer from questions";
$result=mysql_query($query) or die(mysql_error());
$right_answer=0;
$wrong_answer=0;
$unanswered=0;
$i=1;
while($row=mysql_fetch_array($result))
{
if($_POST[$i] == $row['answer'])
{
$right_answer++;
}
else if($_POST[$i]==5)
{
$unanswered++;
}
else
{
$wrong_answer++;
}
$i++;
}
echo "<div id='answer'>";
echo " Right Answer : <span class='highlight'>". $right_answer."</span><br>";

echo " Wrong Answer : <span class='highlight'>". $wrong_answer."</span><br>";

echo " Unanswered Question : <span class='highlight'>". $unanswered."</span><br>";
echo "</div>";
?>


config.php file:

<?php
define('DB_HOST','localhost');
define('DB_NAME','Aptitude');
define('DB_USER','root');
define('DB_PASSWORD','');
$conn=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$conn) or die("Failed to connect to MySQL: " . mysql_error());


?>


Please help me to solve this problem. Thanks in advance.

Answer

since i don't know what was the condition being checked i just wrote a echo statement before the condition and then i came to know that it was checking the value of radio button and not its answer so what i did was i changed the correct answer field in the database to the correct answer's no

while($response=mysql_fetch_array($result))
{      
           echo $response['answer'];//this showed the answer
           echo $_POST["$i"];//this showed the radio button no of the answer clicked
          //so thats why the condition when wrong all the time and so i changed the answer filed to the right answer's radio button number and hence the condition went true
    if($response['answer']==$_POST["$i"])
    {
    $right_answer++;    
    }
    else if($_POST["$i"]==5)
    {   
        $unanswered++;
    }
    else
    {
        $wrong_answer++;
    }
    $i++;
}
Comments