我在点击提交后尝试获取该页面,告诉用户他们是否正确的问题。截至目前,它始终表示正确,无论在文本框中键入的内容是否正确答案。非常感谢任何帮助。
以下是相关代码。第一部分来自创建问题的html文件。
<form action="Graded.php" method="post" id="quiz">
Question 7: How many signs are in the Chinese zodiac?
<input type="text" name="question-7-answers" value="" required>
Question 8: Which is the only reptile that is a sign in the Chinese zodiac?
<input type="text" name="question-8-answers" value="" required>
Question 9: Which is the only imaginary animal that is a sign in the Chinese zodiac?
<input type="text" name="question-9-answers" value="" required>
Question 10: Which is the only bird that is a sign in the Chinese zodiac?
<input type="text" name="question-10-answers" value="" required>
<input type="submit" value="Submit Quiz"/>
<input type="reset" value="Reset Quiz" class="reset_button"/>
</form>
这是php部分,用于检查答案是否正确,并在点击提交后在页面上发布结果。
$answer7 = $_POST['question-7-answers'];
$answer8 = $_POST['question-8-answers'];
$answer9 = $_POST['question-9-answers'];
$answer10 = $_POST['question-10-answers'];
if ($answer7 == "12" || "twelve") { $totalCorrect++; echo "Answer 7 was Correct <br />\n";}
else echo"Incorrect answer. This is the correct answer: Question 7: How many signs are in the Chinese zodiac? Answer: 12 <br />\n";
if ($answer8 == "Snake" || "snake") { $totalCorrect++; echo "Answer 8 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 8: Which is the only reptile that is a sign in the Chinese zodiac? Answer: Snake <br />\n";
if ($answer9 == "Dragon" || "dragon") { $totalCorrect++; echo "Answer 9 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 9: Which is the only imaginary animal that is a sign in the Chinese zodiac? Answer: Dragon <br />\n";
if ($answer10 == "Rooster" || "rooster") { $totalCorrect++; echo "Answer 10 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 10: Which is the only bird that is a sign in the Chinese zodiac? Answer: Rooster <br />\n";
以下是带有固定if语句的更新代码
if ($answer7 == "12" || $answer7 == "twelve") { $totalCorrect++; echo "Answer 7 was Correct <br />\n";}
else echo"Incorrect answer. This is the correct answer: Question 7: How many signs are in the Chinese zodiac? Answer: 12 <br />\n";
if ($answer8 == "Snake" || $answer8 == "snake") { $totalCorrect++; echo "Answer 8 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 8: Which is the only reptile that is a sign in the Chinese zodiac? Answer: Snake <br />\n";
if ($answer9 == "Dragon" || $answer9 == "dragon") { $totalCorrect++; echo "Answer 9 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 9: Which is the only imaginary animal that is a sign in the Chinese zodiac? Answer: Dragon <br />\n";
if ($answer10 == "Rooster" || $answer10 == "rooster") { $totalCorrect++; echo "Answer 10 was Correct <br />\n";}
else echo "Incorrect answer. This is the correct answer: Question 10: Which is the only bird that is a sign in the Chinese zodiac? Answer: Rooster <br />\n";
答案 0 :(得分:2)
您的if
条件错误。
他们应该是if ( $answer7 == "12" || $answer7 == "twelve")
另请注意==
和===
执行不同的比较:How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
更新:您应使用trim
清除任何尾随空格的输入,并使用strtolower
强制小写:
$answer7 = strtolower(trim($answer7));
答案 1 :(得分:0)
您的条件如下:
if ($answer7 == "12" || "twelve")
应该是
if ($answer7 == "12" || $answer7 == "twelve")
或
if ( in_array($answer7, array("12","twelve") )
由于你的第二个条件“十二”,你的支票总是如此。 转换为布尔值的非空字符串被解释为true。
[以完整示例编辑]
$questions = array(
7 => array(
'question' => "How many signs are in the Chinese zodiac ?",
'reponses' => array("12","douze")
),
8 => array(
'question' => "Which is the only reptile that is a sign in the Chinese zodiac ?",
'reponses' => array("snake")
),
9 => array(
'question' => "Which is the only imaginary animal that is a sign in the Chinese zodiac ?",
'reponses' => array("dragon")
)
);
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$totalCorrect = 0;
foreach( $questions as $num => $question ) {
$input = "question-{$num}-answers";
if( isset($_POST[$input]) ) {
if( in_array(strtolower(trim($_POST[$input])), $question['reponses']) ) {
$totalCorrect++;
echo "Answer {$num} was Correct";
}
else
echo "Incorrect answer. This is the correct answer: Question {$num} : {$question['question']} Answer : {$question['reponses'][0]}";
echo "<br />\n"
}
}
}
对于非整数响应,您甚至可以使用 similar_text 或 levenshtein 等函数接受任何小的输入错误。