【问题标题】:PHP checkbox with 2 correct answer带有 2 个正确答案的 PHP 复选框
【发布时间】:2016-10-28 08:38:37
【问题描述】:

我正在做一个测验,下面是一个 html 复选框。

<p>3. The J2EE components includes:</p>
            <p><label for="JavaDevelopmentKit">
               <input type="checkbox" id="JavaDevelopmentKit" name="category[]" value="JavaDevelopmentKit"/>Java Development Kit</label></p>
            <p><label for="VisualStudio">
               <input type="checkbox" id="VisualStudio" name="category[]" value="VisualStudio"/>Uses Visual Studio</label></p>
            <p><label for="WriteOnce">
               <input type="checkbox" id="WriteOnce" name="category[]" value="WriteOnce"/>Write Once Run Anywhere technology</label></p>

这是我的 php 代码

 if ($q3 == ""){
            $errMsg = "<p>You must answer question 3.</p>";
        }
        else if (isset($_POST['JavaDevelopmentKit']) && isset($_POST['WriteOnce'])
                $total++;
            }

我的 PHP 复选框不起作用。

就算我改成

 if ($q3 == ""){
                    $errMsg = "<p>You must answer question 3.</p>";
                }
                else if (isset($_POST['JavaDevelopmentKit'])
                        $total++;
                    }

总计不增加 1。

[编辑]

我按照@mahaidery 告诉我的方式做了,我目前的 php 是

if ($q3 == ""){
    $errMsg = "<p>You must answer question 3.</p>";
}
else if (isset($_POST['category'])){
    $i = 0;
    foreach($_POST['category'] as $k=>$v){
        if (($key == "JavaDevelopmentKit") || ($key == "WriteOnce") || ($key == "Javadatabase") || ($key == "Opendatabase" ) || ($key == "Security")){
            $i++;
        }
    }
    if($i == 5){
        $total++;
    }
    }

显示很多Notice: Undefined variable: key

【问题讨论】:

  • 如果你像默认表单一样发送数据,而不是通过ajax,你需要检查category键而不是JavaDevelopmentKit

标签: php checkbox


【解决方案1】:

你在 PHP 中做错了。 这就是你可以做到的。

<?php
if(isset($_POST['category'])){
    //You can also count the total checked by the following line of code
    //$count = count($_POST['category']);
    //or
    //You can loop thru
    $i=0;
    foreach($_POST['category'] as $k=>$v){
        //$k is the number $v hold the value..
        //you can add the counter here like
        $i++;
    }
    if($i <= 2){
        //do your stuff here
    }
}
?>

【讨论】:

  • 嘿,你能帮帮我吗?我按照你说的编辑了我的 php 代码,但是有错误。谢谢
猜你喜欢
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2022-10-17
相关资源
最近更新 更多