我正在尝试根据我找到的教程创建一个简单的测验。 http://css-tricks.com/building-a-simple-quiz/
不幸的是,这让我转动轮子,答案可能很简单。
我完美地完成了这项工作。我想改变功能。而不是计算答案,我希望它做其他事情。
对我来说,进行测验并说你错过了2并且没有显示错过了什么问题,这对我来说似乎很愚蠢。
我宁愿避免使用数据库。它可以在屏幕上,在新屏幕上或通过电子邮件发送。没有偏好。有什么建议吗?
以下是上述网站的代码:
<form action="grade.php" method="post" id="quiz">
<li>
<h3>CSS Stands for...</h3>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A) Computer Styled Sections </label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B">B) Cascading Style Sheets</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C">C) Crazy Solid Shapes</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D">D) None of the above</label>
</div>
</li>
</form>
<input type="submit" value="Submit Quiz" />
然后是PHP脚本:
<?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>";
?>
非常感谢任何建议或链接。我的谷歌技能让我失望。我想要搜索的所有内容都会带来无关紧要的东西。
答案 0 :(得分:9)
能够回答答案,而不是首先需要存储问题的信件。您不需要使用数据库,只需使用数组即可。
如果要使用数组,我建议将所有内容存储在数组中。由于html的结构是一样的,这可以节省你这么多时间。您可以只编写一次问题并在整个脚本中自动实现。
<?php
$Questions = array(
1 => array(
'Question' => 'CSS stands for',
'Answers' => array(
'A' => 'Computer Styled Sections',
'B' => 'Cascading Style Sheets',
'C' => 'Crazy Solid Shapes'
),
'CorrectAnswer' => 'A'
),
2 => array(
'Question' => 'Second question',
'Answers' => array(
'A' => 'First answer of Second question',
'B' => 'Second answer Second question',
'C' => 'Third answer Second question'
),
'CorrectAnswer' => 'C'
)
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo '<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
} else {
echo '<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
}
echo '<br /><hr>';
}
} else {
?>
<form action="grade.php" method="post" id="quiz">
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<li>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>
关于这个很酷的事情是,如果你想添加另一个问题,你不需要添加任何HTML或任何东西。只需添加问题及其答案,正确的答案,它会自动生效!顺便说一句,这是一个文件,而不是2.所以它应该提交给自己。
答案 1 :(得分:1)
基本结构类似于
if ($answer1 == "B") {
$totalCorrect++;
} else {
$wronganswers[] = "You got #1 wrong. correct answer is B / ...text_of_answer_here ";
}
...
if ($totalCorrect != $number_of_questions) {
echo implode($wronganswers);
}
答案 2 :(得分:0)
对于网页,这是一个简单的经验法则*:
html - 内容
css - 造型
javascript - 行为
只需要在那里找到一些非常简单的JS,而不仅仅是$totalcorrect
,它应该实时更新。
PHP是服务器端的,只在服务器上运行一次(并将您的网页输出到客户端)。 JS是客户端的,只要你在客户端告诉它*就会运行。
(*一般jist,不完全100%正确,但功能正常)
编辑:如果您正在关注PHP教程,这将无济于事
答案 3 :(得分:0)
试试这个:我编辑了Gillian lo wong的代码。我在最后添加了一个分数,但也显示了错误的答案。
<?php
$Questions = array(
1 => array(
'Question' => '1. CSS stands for',
'Answers' => array(
'A' => 'Computer Styled Sections',
'B' => 'Cascading Style Sheets',
'C' => 'Crazy Solid Shapes'
),
'CorrectAnswer' => 'B'
),
2 => array(
'Question' => '2. What is the Capital of the Philippines',
'Answers' => array(
'A' => 'Cebu City',
'B' => 'Davao City',
'C' => 'Manila City'
),
'CorrectAnswer' => 'C'
)
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
} else {
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;
}
echo '<br /><hr>';
if ($counter=="")
{
$counter='0';
$results = "Your score: $counter/2";
}
else
{
$results = "Your score: $counter/2";
}
} echo $results;
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php } ?>
<?php } ?>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>