PHP 测验无法正常工作
PHP Quiz not working properly
我正在尝试做一个简单的 php 测验,但我必须插入答案而不是选择,并将它们与 strcasecmp() 进行比较,这样如果第一个字母是大写就不会有问题或类似的东西,但代码不起作用 properly.Sometimes 它没有 return 正确的结果,即使我插入了正确的答案。
这是代码:
<?php
$number = rand(1,2);
$result = "";
$answer = "";
$question = "";
$question1 = "What is the capital of China";
$question2 = "What king of country is China?";
$answer1 = "beijing";
$answer2 = "republic";
if ($number == 1) {
$answer = $answer1;
$question = $question1;
}
if ($number == 2) {
$answer = $answer2;
$question = $question2;
}
if(isset($_POST['submit'])){
if (strcasecmp($answer,$_POST['answer']) == 0) {
$result = "Correct!";
} else {
$result = "Wrong!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="index.php">
<div class="middle">
<p class="question"><?php echo $question;
?></p>
<p class="result"><?php echo $result;
$result = "" ?></p>
<div class="box">
<input type="text" name="answer" placeholder="Type here" class="text">
</div>
</div>
<input type="submit" name="submit" value="Continue" class="btn">
</form>
</body>
</html>
通过查看您的代码,我们可以看到当您提交表单时,您正在重新启动脚本,这实际上将 $random
重置为一个新值。对于 2 个问题,您有 50% 的机会得到 'correct' 答案,但您会发现添加更多问题后您的脚本根本不起作用。
基本上,您应该使用另一种方法来实现您想要的。您可以尝试在隐藏的 <input>
中添加问题的 ID 并检查您的表单何时提交以确定它是哪一个。
if(isset($_POST['submit'])){
switch ($_POST['question']) { // Add '{'
case 1:
if (strcasecmp($answer1,$_POST['answer']) == 0) {
$result = "Correct!";
} else {
$result = "Gresit!";
}
break;
case 2:
if (strcasecmp($answer2,$_POST['answer']) == 0) {
$result = "Correct!";
} else {
$result = "Gresit!";
} // Forgot to Add '}'
break;
} // Add '}' It give error in PHP 5.3 Parse error: syntax error, unexpected T_CASE, expecting ':' or '{'
}
对于 HTML,您可以在表单中添加此输入:
<input type="text" name="question" value="<?php echo $number ?>" hidden>
这不是实现您想要的效果的最佳方法,这只是可行方法的一个示例。
我正在尝试做一个简单的 php 测验,但我必须插入答案而不是选择,并将它们与 strcasecmp() 进行比较,这样如果第一个字母是大写就不会有问题或类似的东西,但代码不起作用 properly.Sometimes 它没有 return 正确的结果,即使我插入了正确的答案。 这是代码:
<?php
$number = rand(1,2);
$result = "";
$answer = "";
$question = "";
$question1 = "What is the capital of China";
$question2 = "What king of country is China?";
$answer1 = "beijing";
$answer2 = "republic";
if ($number == 1) {
$answer = $answer1;
$question = $question1;
}
if ($number == 2) {
$answer = $answer2;
$question = $question2;
}
if(isset($_POST['submit'])){
if (strcasecmp($answer,$_POST['answer']) == 0) {
$result = "Correct!";
} else {
$result = "Wrong!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="index.php">
<div class="middle">
<p class="question"><?php echo $question;
?></p>
<p class="result"><?php echo $result;
$result = "" ?></p>
<div class="box">
<input type="text" name="answer" placeholder="Type here" class="text">
</div>
</div>
<input type="submit" name="submit" value="Continue" class="btn">
</form>
</body>
</html>
通过查看您的代码,我们可以看到当您提交表单时,您正在重新启动脚本,这实际上将 $random
重置为一个新值。对于 2 个问题,您有 50% 的机会得到 'correct' 答案,但您会发现添加更多问题后您的脚本根本不起作用。
基本上,您应该使用另一种方法来实现您想要的。您可以尝试在隐藏的 <input>
中添加问题的 ID 并检查您的表单何时提交以确定它是哪一个。
if(isset($_POST['submit'])){
switch ($_POST['question']) { // Add '{'
case 1:
if (strcasecmp($answer1,$_POST['answer']) == 0) {
$result = "Correct!";
} else {
$result = "Gresit!";
}
break;
case 2:
if (strcasecmp($answer2,$_POST['answer']) == 0) {
$result = "Correct!";
} else {
$result = "Gresit!";
} // Forgot to Add '}'
break;
} // Add '}' It give error in PHP 5.3 Parse error: syntax error, unexpected T_CASE, expecting ':' or '{'
}
对于 HTML,您可以在表单中添加此输入:
<input type="text" name="question" value="<?php echo $number ?>" hidden>
这不是实现您想要的效果的最佳方法,这只是可行方法的一个示例。