radio button cheched problem
helo sir
i want to tring a quiz app in quiz when i click radio button it checked or when go to next or previous button it show unchecked , i want that when i go next or previos button it show checked .
please help my code bellow
view_exam.php
<?php
//view_exam.php
include('master/Examination.php');
$exam = new Examination;
$exam->user_session_private();
include('header.php');
$exam_id = '';
$exam_status = '';
$remaining_minutes = '';
if(isset($_GET['code']))
{
$exam_id = $exam->Get_exam_id($_GET["code"]);
$exam->query = "
SELECT online_exam_status, online_exam_datetime, online_exam_duration FROM online_exam_table
WHERE online_exam_id = '$exam_id'
";
$result = $exam->query_result();
foreach($result as $row)
{
$exam_status = $row['online_exam_status'];
$exam_star_time = $row['online_exam_datetime'];
$duration = $row['online_exam_duration'] . ' minute';
$exam_end_time = strtotime($exam_star_time . '+' . $duration);
$exam_end_time = date('Y-m-d H:i:s', $exam_end_time);
$remaining_minutes = strtotime($exam_end_time) - time();
}
}
else
{
header('location:enroll_exam.php');
}
?>
<br />
<?php
if($exam_status == 'Started')
{
$exam->data = array(
':user_id' => $_SESSION['user_id'],
':exam_id' => $exam_id,
':attendance_status' => 'Present'
);
$exam->query = "
UPDATE user_exam_enroll_table
SET attendance_status = :attendance_status
WHERE user_id = :user_id
AND exam_id = :exam_id
";
$exam->execute_query();
?>
<div class="row">
<div class="col-md-8">
<div class="card">
<div class="card-header">Online Exam</div>
<div class="card-body">
<div id="single_question_area"></div>
</div>
</div>
<br />
<div id="question_navigation_area"></div>
</div>
<div class="col-md-4">
<br />
<div align="center">
<div id="exam_timer" data-timer="<?php echo $remaining_minutes; ?>" style="max-width:400px; width: 100%; height: 200px;"></div>
</div>
<br />
<div id="user_details_area"></div>
</div>
</div>
<script>
$(document).ready(function(){
var exam_id = "<?php echo $exam_id; ?>";
load_question();
question_navigation();
function load_question(question_id = '')
{
$.ajax({
url:"user_ajax_action.php",
method:"POST",
data:{exam_id:exam_id, question_id:question_id, page:'view_exam', action:'load_question'},
success:function(data)
{
$('#single_question_area').html(data);
}
})
}
$(document).on('click', '.next', function(){
var question_id = $(this).attr('id');
load_question(question_id);
});
$(document).on('click', '.previous', function(){
var question_id = $(this).attr('id');
load_question(question_id);
});
function question_navigation()
{
$.ajax({
url:"user_ajax_action.php",
method:"POST",
data:{exam_id:exam_id, page:'view_exam', action:'question_navigation'},
success:function(data)
{
$('#question_navigation_area').html(data);
}
})
}
$(document).on('click', '.question_navigation', function(){
var question_id = $(this).data('question_id');
load_question(question_id);
});
function load_user_details()
{
$.ajax({
url:"user_ajax_action.php",
method:"POST",
data:{page:'view_exam', action:'user_detail'},
success:function(data)
{
$('#user_details_area').html(data);
}
})
}
load_user_details();
$("#exam_timer").TimeCircles({
time:{
Days:{
show: false
},
Hours:{
show: false
}
}
});
setInterval(function(){
var remaining_second = $("#exam_timer").TimeCircles().getTime();
if(remaining_second < 1)
{
alert('Exam time over');
location.reload();
}
}, 1000);
$(document).on('click', '.answer_option', function(){
var question_id = $(this).data('question_id');
var answer_option = $('input[name=option_1]:checked').val();
$.ajax({
url:"user_ajax_action.php",
method:"POST",
data:{question_id:question_id, answer_option:answer_option, exam_id:exam_id, page:'view_exam', action:'answer'},
success:function(data)
{
}
})
});
});
</script>
user_ajax.php
if($_POST['page'] == 'view_exam')
{
if($_POST['action'] == 'load_question')
{
if($_POST['question_id'] == '')
{
$exam->query = "
SELECT * FROM question_table
WHERE online_exam_id = '".$_POST["exam_id"]."'
ORDER BY question_id ASC
LIMIT 1
";
}
else
{
$exam->query = "
SELECT * FROM question_table
WHERE question_id = '".$_POST["question_id"]."'
";
}
$result = $exam->query_result();
$output = '';
foreach($result as $row)
{
$output .= '
<h1>'.$row["question_title"].'</h1>
<hr />
<br />
<div class="row">
';
$exam->query = "
SELECT * FROM option_table
WHERE question_id = '".$row['question_id']."'
";
$sub_result = $exam->query_result();
$count = 1;
foreach($sub_result as $sub_row)
{
$output .= '
<div class="col-md-6" style="margin-bottom:32px;">
<div class="radio">
<label><h4><input type="radio" name="option_1" class="answer_option" data-question_id="'.$row["question_id"].'" id-data="'.$count.'"/> '.$sub_row["option_title"].'</h4></label>
</div>
</div>
';
$count = $count + 1;
}
$output .= '
</div>
';
$exam->query = "
SELECT question_id FROM question_table
WHERE question_id < '".$row['question_id']."'
AND online_exam_id = '".$_POST["exam_id"]."'
ORDER BY question_id DESC
LIMIT 1";
$previous_result = $exam->query_result();
$previous_id = '';
$next_id = '';
foreach($previous_result as $previous_row)
{
$previous_id = $previous_row['question_id'];
}
$exam->query = "
SELECT question_id FROM question_table
WHERE question_id > '".$row['question_id']."'
AND online_exam_id = '".$_POST["exam_id"]."'
ORDER BY question_id ASC
LIMIT 1";
$next_result = $exam->query_result();
foreach($next_result as $next_row)
{
$next_id = $next_row['question_id'];
}
$if_previous_disable = '';
$if_next_disable = '';
if($previous_id == "")
{
$if_previous_disable = 'disabled';
}
if($next_id == "")
{
$if_next_disable = 'disabled';
}
$output .= '
<br /><br />
<div align="center">
<button type="button" name="previous" class="btn btn-info btn-lg previous" id="'.$previous_id.'" '.$if_previous_disable.'>Previous</button>
<button type="button" name="next" class="btn btn-warning btn-lg next" id="'.$next_id.'" '.$if_next_disable.'>Next</button>
</div>
<br /><br />';
}
echo $output;
}
if($_POST['action'] == 'question_navigation')
{
$exam->query = "
SELECT question_id FROM question_table
WHERE online_exam_id = '".$_POST["exam_id"]."'
ORDER BY question_id ASC
";
$result = $exam->query_result();
$output = '
<div class="card">
<div class="card-header">Question Navigation</div>
<div class="card-body">
<div class="row">
';
$count = 1;
foreach($result as $row)
{
$output .= '
<div class="col-md-2" style="margin-bottom:24px;">
<button type="button" class="btn btn-primary btn-lg question_navigation" data-question_id="'.$row["question_id"].'">'.$count.'</button>
</div>
';
$count++;
}
$output .= '
</div>
</div></div>
';
echo $output;
}
if($_POST['action'] == 'user_detail')
{
$exam->query = "
SELECT * FROM user_table
WHERE user_id = '".$_SESSION["user_id"]."'
";
$result = $exam->query_result();
$output = '
<div class="card">
<div class="card-header">User Details</div>
<div class="card-body">
<div class="row">
';
foreach($result as $row)
{
$output .= '
<div class="col-md-3">
<img src="upload/'.$row["user_image"].'" class="img-fluid" />
</div>
<div class="col-md-9">
<table class="table table-bordered">
<tr>
<th>Name</th>
<td>'.$row["user_name"].'</td>
</tr>
<tr>
<th>Email ID</th>
<td>'.$row["user_email_address"].'</td>
</tr>
<tr>
<th>Gendar</th>
<td>'.$row["user_gender"].'</td>
</tr>
</table>
</div>
';
}
$output .= '</div></div></div>';
echo $output;
}
if($_POST['action'] == 'answer')
{
$exam_right_answer_mark = $exam->Get_question_right_answer_mark($_POST['exam_id']);
$exam_wrong_answer_mark = $exam->Get_question_wrong_answer_mark($_POST['exam_id']);
$orignal_answer = $exam->Get_question_answer_option($_POST['question_id']);
$marks = 0;
if($orignal_answer == $_POST['answer_option'])
{
$marks = '+' . $exam_right_answer_mark;
}
else
{
$marks = '-' . $exam_wrong_answer_mark;
}
$exam->data = array(
':user_answer_option' => $_POST['answer_option'],
':marks' => $marks
);
$exam->query = "
UPDATE user_exam_question_answer
SET user_answer_option = :user_answer_option, marks = :marks
WHERE user_id = '".$_SESSION["user_id"]."'
AND exam_id = '".$_POST['exam_id']."'
AND question_id = '".$_POST["question_id"]."'
";
$exam->execute_query();
}
}
}