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

标签 javascript php jquery html ajax

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

这是一个测验网站。问题是使用 ajax.php 中的 sql 获取的,并使用 ajax 方法调用到 index.php 中。当用户完成所有问题时,使用 session (位于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中时间结束时显示分数。现在当用户完成问题时显示分数。当时间为 00 时 我需要显示分数,直到用户回答问题为止。

有人可以帮助我吗?

最佳答案

在发送 javascript AJAX 请求时,您可以创建一个变量“show_result”并将其默认值传递为 0。每当时间结束时,发送另一个 javascript AJAX 请求,其中变量“show_result”为 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']})");
    }
}

关于javascript - 从一个页面调用 session 到另一个页面并将其包含在 javascript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50713234/

相关文章:

javascript - Angular js : How to use resolve when using with require js

javascript - 两个仪表在一整圈内

javascript - 自定义管道未正确导入

php - 如何将TTF字体添加到html2pdf PHP程序中

php - 无法将远程 Openfiles.exe 数据拉入 PHP

jquery 选择器语法 $( [] ) 和 $ ("*")

javascript - 如何重写 Javascript 代码引用的 URL?

php - CakePHP 身份验证后访问受限文件

javascript - YouTube API 和 Websockets 确保两个视频同步(视频 1 在视频 2 缓冲时暂停)

javascript - 使用 CDN 的 Google PageSpeed Insight CSS Javascript "above the fold"问题