javascript - 从一个页面调用会话到另一个页面并将其包含在 javascript 中
问题描述
我的 php 文件 ajax.php 中有一些 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 中显示分数。现在当用户完成问题时会显示分数。当时间为 00 时,我需要显示分数,直到问题用户参加。
谁能帮我?
解决方案
在发送 javascript AJAX 请求时,您可以创建一个变量为“show_result”并将其默认值传递为 0。每当时间过去,发送另一个变量“show_result”为 1 的 javascript AJAX 请求。
并在 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']})");
}
}
推荐阅读
- php - 在 PHP 中导入 CSV 验证
- spring - 一起使用 Spring `@Scheduled` 和 `@Async` 的设计注意事项
- javascript - 传递联合类型的道具以反应需要联合中的一种类型的子组件
- javascript - Javascript - 在对象中动态创建数组并推送到它
- swift - 如何对我的 TODO 进行分类?
- vuejs2 - 如何在 vue js 和 laravel api 中使用删除方法
- sql - 我对数据时间有什么用?
- android - React Native 错误:多个 dex 文件在生成生产 apk 时定义 Lcom/facebook/binaryresource/BinaryResource
- ruby - '&:' 与 'is_a(Integer)' 一起使用
- javascript - Bookshelf.js 获取具有相关(连接)表的行 - 限制相关行的数量