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?

6
  • Hi, can you explain the question in details? Commented Jun 6, 2018 at 6:21
  • i need to display the score Commented Jun 6, 2018 at 6:28
  • Your score is in PHP session , and you want it in javascript? Commented Jun 6, 2018 at 6:29
  • right now when the user finishes all the questions, the score is displayed in index.php from session in ajax.php. i need to display the score if the user runs out of time Commented Jun 6, 2018 at 6:29
  • Refere this answer stackoverflow.com/questions/41391862/… Commented Jun 6, 2018 at 6:30

2 Answers 2

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']})");
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

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); } "
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.
can you please open a chat
can you give me ur mail id or something. i didnt clearly understand
|
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";
                                 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.