我正在尝试设置从https://css-tricks.com/building-a-simple-quiz/#comments获得的表单,以便如果所有答案均不正确,则会在结果中添加“失败”,并提供一个按钮以重试。但是,如果所有答案都是正确的,则会显示“通过”和一个继续进入新页面的按钮
虽然我承认我不擅长编码和脚本编写,但我尝试仅通过链接更改回显,但这并没有为我要查找的每个结果提供不同的显示。他们几乎必须正确回答所有答案才能前进,或者可以重试。
这是我正在使用的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Quiz</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div id="page-wrap">
<h1>Final Quiz for Lip building</h1>
<?php
$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];
$answer4 = $_POST['question-4-answers'];
$answer5 = $_POST['question-5-answers'];
$totalCorrect = 0;
if ($answer1 == "B") { $totalCorrect++; }
if ($answer2 == "A") { $totalCorrect++; }
if ($answer3 == "C") { $totalCorrect++; }
if ($answer4 == "D") { $totalCorrect++; }
if ($answer5) { $totalCorrect++; }
echo "<div id='results'>$totalCorrect / 5 correct</div>";
?>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-68528-29");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>
________
AND the PHP (grade.php) file
________
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<?php
$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];
$answer4 = $_POST['question-4-answers'];
$answer5 = $_POST['question-5-answers'];
$totalCorrect = 0;
if ($answer1 == "B") { $totalCorrect++; }
if ($answer2 == "A") { $totalCorrect++; }
if ($answer3 == "C") { $totalCorrect++; }
if ($answer4 == "D") { $totalCorrect++; }
if ($answer5) { $totalCorrect++; }
echo "<div id='results'>$totalCorrect / 5 correct</div>";
?>
</body>
</html>
没有得到任何错误
答案 0 :(得分:0)
简单的if语句就能解决问题
您可以在此行下的grade.php中添加此更改
echo "<div id='results'>$totalCorrect / 5 correct</div>";
通过检查用户获得的分数来检查用户是否通过了测试
if($totalCorrect == 5){
echo "<div id='results'>Pass</div>";
echo '<button type="button" href="redirect.php">Click Me!</button>';
} else {
echo "<div id='results'>Fail</div>";
}