测验中的PHP复选框,每个人必须选择多个正确答案才能得到一分

测验中的PHP复选框,每个人必须选择多个正确答案才能得到一分,php,html,arrays,Php,Html,Arrays,我目前正在做一个关于html和PHP的测验,我想在有多个正确答案的地方使用复选框。用户必须获得所有正确答案,才能在每个问题上获得一分。我一直在看很多教程,但似乎没有什么完全符合我的需要。任何帮助都将不胜感激 以下是其中一个问题的示例,如果您能告诉我需要添加什么: <p class="question">2. Which clubs did Arsene Wenger manage before taking the arsenal job in 1996?</p> <

我目前正在做一个关于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>

2。在1996年接受阿森纳的工作之前,温格管理过哪些俱乐部

    摩纳哥
    巴黎圣日耳曼
    拜仁慕尼黑
    名古屋祖父
在一个单独的答案页面上,我现在有:

<?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
//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;
   }
}
?>
对于问题三,正确的答案可能是复选框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;
   }
}
?>

您的输入是数组,因此您的PHP需要将它们视为数组;对不起,我对php完全陌生(从昨天开始),你能再解释一下吗?谢谢你这么快的回答:)非常感谢!我知道这很简单,这是一个很好的答案。谢谢你多花一点时间帮忙:)@JoshStrachan我很高兴。祝你编码好运!嘿,我几乎完全完成了我的代码,但我想知道你是否可以快速帮助我,或者用其他一些东西为我指出正确的方向?我想知道是否有办法禁用提交按钮,直到所有表单都被答复?第二,我的测验超过2页,我如何移动到下一页并保存答案,以便我可以在第二页末尾提交要禁用提交按钮,您需要类似的东西,因此:您需要修改它,以便检查每个字段,但同时要小心,不要要求检查所有内容,否则用户将无法继续。类似的内容也可能有帮助:。要使表单覆盖多个页面,您需要有两个页面,每个页面上有一个表单,第一个页面有十个问题,当您提交此问题时,它会将其发布到第二个页面,第二个页面确保结果,将其存储在一个隐藏字段中,随后在提交第二份表格(即接下来的10个问题)时提交该字段。另一种可能是使用会话,请参见此处:但是,两个请求都可能需要一个新问题。编码的一个缺点是,想法需要几秒钟,实现完全是另一回事。
 if(($key == 0 && $a2) || ($key == 3 && $a2)
<?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;
   }
}
?>
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;
  }
}