0

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?

Elydasian
  • 2,016
  • 5
  • 23
  • 41

2 Answers2

1

while sending javascript AJAX request, you can create a variable as "show_result" and pass it's default value as 0. Whenever time gets over, send another javascript AJAX request with variable "show_result" as 1.

And do below changes in ajax.php

if( isset($_POST['show_result']) && ($_POST['show_result'] == 1) ) {
    // default value for show_result POST variable will be 0 & it will become only 1 when all questions are done or time is over.
    // this value needs to passed from javascript, depending on time remaining value. If time is over then pass it as 1 else pass it as 0
    // Time over, show results
    echo"<div>
    <h2>Results:</h2>
    <p>Correct answers: {$_SESSION['correct_score']}</p>
    <p>Wrong answers: {$_SESSION['not_correct_score']}</p>

    </div>";
}
else {
    $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']})");
    }
}
Prasad Wargad
  • 737
  • 2
  • 7
  • 11
0

I have found a solution. I made an extra page score.php and did the following to store session and print its value:

<?php

   $session_value=(isset($_SESSION['correct_score']))?$_SESSION['correct_score']:'';
   $session_value2=(isset($_SESSION['not_correct_score']))?$_SESSION['not_correct_score']:'';
   echo "<div class='div-left'> Number Of Correct Answers = " . $session_value . "</div>";
   echo "<div class='div-left'> <br>Number Of Wrong Answers = " . $session_value2 . "</div>";
    ?>

and in the javascript in the index.php i did this to redirect to score.php if time out:

 if (parseInt(sec) == 0) {
                                 min = parseInt(min) - 1;
                                 if (parseInt(min) == 0) {
                                         clearTimeout(tim);
                                         location.href = "score.php";
                                 }