【问题标题】:PHP quiz with on screen results带有屏幕结果的 PHP 测验
【发布时间】:2013-03-18 16:40:26
【问题描述】:

我正在尝试根据我找到的教程创建一个简单的测验。 http://css-tricks.com/building-a-simple-quiz/

不幸的是,这让我在旋转我的车轮,答案可能很简单。

我得到了这个完美的工作。我想更改功能。我希望它做其他事情,而不是计算答案。

  1. 我想再次显示问题。
  2. 还有,选择的答案和正确的答案。回显答案,不是字母 A、B、C、D。

在我看来,做一个小测验并说你错过了 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>";

?>

任何建议或链接将不胜感激。我的谷歌技能让我失望了。我想搜索的所有内容都会带来不相关的内容。

【问题讨论】:

  • 您必须找到一种方法将grade.php 中的函数合并到index.php 测验文件中,并将表单操作设置为$_SERVER['PHP_SELF'];,和/或在某处设置include file那里(也许在一个 DIV 中)有测验问题。除此之外,它还需要一些漂亮的编码。我可能会成功,但这需要我一些时间。好问题。

标签: php html forms


【解决方案1】:

为了能够回显答案,而不是字母,您需要先存储问题。你不需要使用数据库,你可以使用一个数组。

如果您要使用数组,我建议将所有内容存储在数组中。由于 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个。所以它应该提交给自己。

【讨论】:

  • 谢谢阿娇,效果很好。将表单操作从 grade.php 更改为 &lt;?php echo $_SERVER['PHP_SELF']; ?&gt; "C B"
  • 非常感谢大家!
  • 阿娇,这是一个很棒的解决方案。我确实接受了 Fred 的建议,效果很好。我对一个部分很好奇。使用 if else,它以红色返回不正确的。是否也可以返回正确答案(黑色或绿色无所谓)?
  • 在该 foreach 中,正确答案存储在 $Value['CorrectAnswer'] 中。用户提供的答案存储在 $Answers[$QuestionNo] 中。谢谢
  • 用大写来命名变量和数组是不是很混乱?
【解决方案2】:

基本结构类似于

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);
}

【讨论】:

    【解决方案3】:

    以下是网页的简单经验法则*:

    html - 内容

    css - 样式

    javascript - 行为

    只需在其中添加一些非常简单的 JS,而不仅仅是 $totalcorrect,它应该会实时更新。

    PHP 是服务器端的,只在服务器上运行一次(并将您的网页输出到客户端)。 JS 是客户端的,只要您告诉它在客户端上运行,它就会一直运行。

    (*一般性原则,并非完全 100% 正确,但功能齐全)

    编辑:如果您正在学习 PHP 教程,这将无济于事

    【讨论】:

      【解决方案4】:

      试试这个:我编辑了 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 
          }
          ?>
      

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 1970-01-01
        • 2017-12-23
        • 2019-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        相关资源
        最近更新 更多