我正在学习编程,所以初学者。 下面我尝试用php和html创建一个简单的测验。每个问题都需要刷新(每页1个问题)。我正在尝试记录总分,但是一旦刷新页面(加载新问题),之前的分数就会消失。
你能否告诉我我做错了什么以及我应该做些什么才能让它发挥作用?
谢谢。
<?php
// get $pos from address bar
$pos = $_GET['position'];
if (!$pos) $pos = 0;
// showing the position (just to see to test.)
echo "position is $pos <br>";
$questions = array (
//Array for questions which include qID, calcType, qLayout, numbers (num1, num2,etc.), correctAns.
array (1, "Addition question", "add", "horizontal", array(1, 2, 3), array(1, "altAns"), array("hint1","hint2","hint3")),
array (1, "Addition question", "add", "horizontal", array(4, 5, 6), array(1, "altAns"), array("hint1","hint2","hint3")),
array (1, "Addition question", "add", "horizontal", array(7, 8, 9), array(1, "altAns"), array("hint1","hint2","hint3"))
);
$numOfQs= (sizeof($questions));
?>
<!doctype html>
<html>
<head>
<title>Maths Quiz</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<style>
.questionForm {
border:1px solid grey;
border-radius: 10px;
margin-top: 20px;
}
form {
padding-bottom: 20px;
}
p {
font-size:20px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 questionForm">
<form method="POST">
<div class="form-group">
<p>
<?php
// Generate question here.
echo "Question ".($pos+1)." of ".$numOfQs;
$question = $questions[$pos][1];
$calcType = $questions[$pos][2];
$qLayout = $questions[$pos][3];
$num1 = $questions[$pos][4][0];
$num2 = $questions[$pos][4][1];
$correctAns = $num1 + $num2;
echo "<h3>$question<br>".$num1." + ".$num2." = ? </h3>";
?>
</p>
<label for="yourAns">Your answer:</label>
<input type="text" name="yourAns" class="form-control" placeholder="Answer here." />
</div>
<input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit" />
</form>
<div>
<p><?php
if ($_POST["submit"]) {
if($_POST["yourAns"]) {
$yourAns=$_POST["yourAns"];
if ($yourAns == $correctAns) {
echo "Your answer is correct!";
$correct[$pos] = 1;
} else {
echo "Your answer is wrong!</br> Correct answer is ".$correctAns.".";
$correct[$pos] = 0;
}
} else {
echo "Please enter your answer.";
}
// link to $pos + 1
echo '<br><a href="index.php?position='. ($pos+1) .'">Next page</a><br>';
$totalCorrect=$correct[0]+$correct[1]+$correct[2];
}
echo "Total correct is $totalCorrect <br>";
?> </p>
</div>
</div>
</div>
</div>
<?php
echo "Total correct is $totalCorrect <br>";
print_r($correct);
?>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
答案 0 :(得分:0)
您可以使用$ _SESSION将总数发送到另一个问题页面(不会因“刷新”而丢失值)。
<?php
$totalCorrect = 0;
session_start();
// get $pos from address bar
$pos = $_GET['position'];
if (!$pos) $pos = 0;
echo "position is $pos <br>";// showing the position (just to see to test.)
$questions = array (
//Array for questions which include qID, calcType, qLayout, numbers (num1, num2,etc.), correctAns.
array (1, "Addition question", "add", "horizontal", array(1, 2, 3), array(1, "altAns"), array("hint1","hint2","hint3")),
array (1, "Addition question", "add", "horizontal", array(4, 5, 6), array(1, "altAns"), array("hint1","hint2","hint3")),
array (1, "Addition question", "add", "horizontal", array(7, 8, 9), array(1, "altAns"), array("hint1","hint2","hint3"))
);
$numOfQs= (sizeof($questions));
?>
<!doctype html>
<html>
<head>
<title>Maths Quiz</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<style>
.questionForm {
border:1px solid grey;
border-radius: 10px;
margin-top: 20px;
}
form {
padding-bottom: 20px;
}
p {
font-size:20px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 questionForm">
<form method="POST">
<div class="form-group">
<p>
<?php
// Generate question here.
echo "Question ".($pos+1)." of ".$numOfQs;
$question = $questions[$pos][1];
$calcType = $questions[$pos][2];
$qLayout = $questions[$pos][3];
$num1 = $questions[$pos][4][0];
$num2 = $questions[$pos][4][1];
$correctAns = $num1 + $num2;
echo "<h3>$question<br>".$num1." + ".$num2." = ? </h3>";
?>
</p>
<label for="yourAns">Your answer:</label>
<input type="text" name="yourAns" class="form-control" placeholder="Answer here." />
</div>
<input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit" />
</form>
<div>
<p><?php
if ($_POST["submit"]) {
if($_POST["yourAns"]) {
$yourAns=$_POST["yourAns"];
if ($yourAns == $correctAns) {
echo "Your answer is correct!";
$_SESSION['correct'][$pos] = 1;
} else {
echo "Your answer is wrong!</br> Correct answer is ".$correctAns.".";
$_SESSION['correct'][$pos] = 0;
}
} else {
echo "Please enter your answer.";
}
// link to $pos + 1
echo '<br><a href="?position='. ($pos+1) .'">Next page</a><br>';
//$totalCorrect=$correct[0]+$correct[1]+$correct[2];
$totalCorrect = $_SESSION['correct'][0] + $_SESSION['correct'][1] + $_SESSION['correct'][2];
}
echo "Total correct is $totalCorrect<br>";
?> </p>
</div>
</div>
</div>
</div>
<?php
//echo "Total correct is {$_SESSION['totalCorrect']} <br>";
// print_r($_SESSION['totalCorrect']);
?>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
我希望这有帮助!
答案 1 :(得分:0)
使用php session
http://php.net/manual/en/book.session.php
它将处理php页面中的值,即使你刷新页面也不会重置它,除非你将它设置为重置值。
答案 2 :(得分:0)
您未在表单中发布位置参数。
您已获得get参数<a href="....?position=..">
但是当您点击该链接时,该链接不会提交表单,并且用户的答案无法提交。
您可以通过将$_GET[..]
更改为$_POST[..]
,删除链接并使用隐藏输入来传递位置参数以便发布而不是获取(获取?)来修复此问题。