从一个页面调用会话到另一个页面并将其包含在javascript中

时间:2018-06-06 06:08:34

标签: javascript php jquery html ajax

我的php文件中有一些php代码ajax.php以及我的index.php中的一些html和javascript。

这是一个测验网站。问题是使用ajax.php中的sql获取的,并使用ajax方法调用index.php。当用户完成所有问题时,使用会话(在ajax.php中)显示分数。

以下是我的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']})");
?>

下面是我的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>

我希望在index.php.right中时间结束时显示分数,现在当用户完成问题时会显示分数。当时间是00 我需要显示分数直到问题用户参加。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

发送javascript AJAX请求时,你可以创建一个变量&#34; show_result&#34;并将其默认值传递为0.每当时间结束时,发送另一个带有变量&#34; show_result&#34;的javascript AJAX请求。为1。

在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']})");
    }
}

答案 1 :(得分:0)

我找到了解决方案。我做了一个额外的页面得分.php并执行以下操作来存储会话并打印其值:

<?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>";
    ?>

并且在index.php的javascript中,如果超时,我会将其重定向到score.php:

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