I have a some php code in my php file ajax.php and some html and javascript in my index.php.
It is a quiz website. The questions are fetched using sql in ajax.php and called into index.php using ajax method. When the user finishes all the questions, the score is displayed using session(which is in ajax.php).
Below is some part of my ajax.php:
$response = mysqli_query($con, "select * from haad");
$number_of_all_questions = mysqli_num_rows($response);
if ($_POST['next_id'] == 0) {
// reset to default
$_SESSION["correct_score"] = 0;
$_SESSION["not_correct_score"] = 0;
}
if ($number_of_all_questions <= $_POST['next_id']) {
// Quiz finished, show results
echo"<div>
< h2 > Results:</h2 >
<p>Correct answers: {$_SESSION['correct_score']}</p>
<p>Wrong answers: {$_SESSION['not_correct_score']}</p>
</div > ";
} else {
// query next question
$response = mysqli_query($con, "select * from haad WHERE id =(select min(id) from questions where id > {$_POST['next_id']})");
?>
Below is my index.php:
<script language="javascript">
var tim;
var min = 2;
var sec = 01;
var f = new Date();
function f1() {
f2();
document.getElementById("starttime").innerHTML = "You Started Your Exam At " + f.getHours() + ":" + f.getMinutes();
}
function f2() {
if (parseInt(sec) > 0) {
sec = parseInt(sec) - 1;
document.getElementById("showtime").innerHTML = "Time Left :<br>" + min + " Minutes ," + sec + " Seconds";
tim = setTimeout("f2()", 1000);
} else {
if (parseInt(sec) == 0) {
min = parseInt(min) - 1;
if (parseInt(min) == 0) {
clearTimeout(tim);
// location.href = "https://google.com";
} else {
sec = 60;
document.getElementById("showtime").innerHTML = "Time Left :<br>" + min + " Minutes ," + sec + " Seconds";
tim = setTimeout("f2()", 1000);
}
}
}
}
setTimeout(function() {
alert("5 minutes remaining");
}, 1000000);
</script>
I want to display the score if the time is over in index.php.right now the score is displayed when the user finishes the questions. When the time is 00 I need to display the score till the question user attended.
Can anyone help me?