【问题标题】:PHP checkboxes in quiz, where person must select multiple correct answers to get one mark测验中的 PHP 复选框,人们必须选择多个正确答案才能获得一分
【发布时间】:2015-05-05 12:26:31
【问题描述】:

我目前正在使用 html 和 PHP 进行测验,我想在有多个正确答案的情况下使用复选框。用户必须得到所有正确答案才能获得每题一分。我一直在看很多教程,但似乎没有什么能完全满足我的需求。任何帮助将不胜感激!

这是其中一个问题的示例,如果您能告诉我需要添加什么:

<p class="question">2. Which clubs did Arsene Wenger manage before taking the arsenal job in 1996?</p>
<ul class="answers">
    <input type="checkbox" name="q2[]" id="q2a" value="q2a"><label for="q2a" class="labela">Monaco</label><br/>
    <input type="checkbox" name="q2[]" id="q2b" value="q2b"><label for="q2b" class="labelb">Paris Saint-Germain</label><br/>
    <input type="checkbox" name="q2[]" id="q2c" value="q2c"><label for="q2c" class="labelc">Bayern Munich</label><br/>
    <input type="checkbox" name="q2[]" id="q2d" value="q2d"><label for="q2d" class="labeld">Nagoya Grampus</label><br/>
</ul>

在一个单独的答案页面上我现在拥有什么:

<?php
$q2 = $_POST['q2'];

    $total = 0;

    if ($q2 == "a") { $total = $total + 0.5; }
    if ($q2 == "b") { $total = $total + 0; }
    if ($q2 == "c") { $total = $total + 0; }
    if ($q2 == "d") { $total = $total + 0.5; }
?>

【问题讨论】:

  • 您的输入是数组,因此您的 PHP 需要将它们视为数组;你没有那样做。
  • 对不起,我对 php 完全陌生(截至昨天),所以你能解释一下吗?谢谢你这么快回答:)

标签: php html arrays


【解决方案1】:

您可以执行类似于以下的操作:

<?php
//This should contain four array elements 0-3
$q2 = $_POST['q2'];

$total = 0;

foreach($q2 as $key=>$a2)
{
   //Check if this is the first or fourth checkbox and that it is ticked
   if(($key == 0 && $a2) || ($key == 3 && $a2)
   { 
      //We have checkbox 1 or 4 and they have been checked
      $total = $total + 0.5;
   }
}
?>

唯一的问题是,您必须遍历每个问题并每次更改循环,以便您只选择正确的答案。即下面的行:

 if(($key == 0 && $a2) || ($key == 3 && $a2)

对于问题三,正确的答案可能是复选框 1 和 2,因此脚本开始看起来像这样

<?php
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];

$total = 0;

foreach($q2 as $key=>$a2)
{
   //Check if this is the first or fourth checkbox and that it is ticked
   if(($key == 0 && $a) || ($key == 3 && $a)
   { 
      //We have checkbox 1 or 4 and they have been checked
      $total = $total + 0.5;
   }
}

foreach($q3 as $key=>$a3)
{
   //Check if this is the first or second checkbox and that it is ticked
   if(($key == 0 && $a3) || ($key == 1 && $a3)
   { 
      //We have checkbox 1 or 2 and they have been checked
      $total = $total + 0.5;
   }
}
?>

这意味着如果是 20 个问题的测验,您还有一些工作要做。另请注意,如果他们选中不是正确答案的框,他们将不会失去任何分数。因此,如果我勾选页面上的每个复选框,我将获得 100% 的分数,即使我可能选中了一个错误答案的框,要解决这个问题,如果选中了错误的框,你可以减去,如下所示:

foreach($q3 as $key=>$a3)
{
   //Check if this is the first or second checkbox and that it is ticked
   if(($key == 0 && $a3) || ($key == 3 && $a3)
   { 
      //We have checkbox 1 or 2 and they have been checked
      $total = $total + 0.5;
   }
   else
  {
     $total = $total - 0.5;
  }
}

【讨论】:

  • 非常感谢,这是一个很好的答案。感谢您加倍努力提供帮助:)
  • @JoshStrachan 我很高兴。祝你编码顺利!
  • 嘿,我的代码几乎完全完成了,但我想知道您是否可以快速帮助我或通过其他几件事为我指明正确的方向?我想知道是否有办法在所有表单都得到答复之前禁用提交按钮?其次,我的测验超过 2 页,我将如何移动到下一页并保存答案,以便我可以在第二页末尾提交
  • 要禁用提交按钮,您需要类似于以下内容:stackoverflow.com/questions/16761696/… 您需要修改它以便检查每个字段,但同时要小心说您没有要求检查所有内容,否则用户将无法继续。这样的事情也可能有所帮助:stackoverflow.com/questions/2684434/….
  • 要将您的表单放在多个页面上,您需要说两页和一个表单,第一页有十个问题,当您提交时,它会将其发布到第二页,第二页确保结果,将它们存储在一个隐藏字段中,随后在提交第二个表单时提交,即接下来的 10 个问题。另一种可能性是使用会话,请参见此处:html-form-guide.com/php-form/php-order-form.html 然而,这两个请求都可能需要一个新问题。编码的一件坏事,想法需要几秒钟,实施完全是另一锅鱼。
【解决方案2】:

如果元素名称中包含[],即为数组,则必须检查值是否在数组中in_array()

if (in_array("a",$q2)) { $total = $total + 0.5; }

【讨论】:

  • 非常感谢!知道这很简单
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2022-06-22
  • 2014-02-23
相关资源
最近更新 更多