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
  • this is my else statement if time gets over can you tell me how to modify it "else { if (parseInt(sec) == 0) { min = parseInt(min) - 1; if (parseInt(min) == 0) { clearTimeout(tim); } " – Zubair Nazer Oliyat Jun 06 '18 at 06:43
  • you might have created a javascript function for AJAX call, create a parameter in that function with default value as 0. Refer below javascript function. function fetch_next_request(show_result = 0) { // AJAX code goes here $.ajax({ url: "URL", data: {show_result: show_result}, error: function() { // code for erroneous request }, success: function() { // code when request gets completed } }); } now when you are doing clearTimeout(tim);, just call this function like fetch_next_request(1); which makes sure that variable show_result value goes 1. – Prasad Wargad Jun 06 '18 at 06:49
  • can you please open a chat – Zubair Nazer Oliyat Jun 06 '18 at 06:53
  • can you give me ur mail id or something. i didnt clearly understand – Zubair Nazer Oliyat Jun 06 '18 at 06:59
  • prasad.wa@neuralit.com – Prasad Wargad Jun 06 '18 at 07:00
  • i ve sent u a mail – Zubair Nazer Oliyat Jun 06 '18 at 07:02
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";
                                 }