简体   繁体   English

PHP复选框,带2个正确答案

[英]PHP checkbox with 2 correct answer

I'm doing a quiz the below is a html checkbox. 我正在做一个测验,下面是一个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>

this is my php code 这是我的PHP代码

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

My PHP for checkbox is not working. 我的PHP for复选框无效。

Even if I change it to 即使我把它改成了

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

Total is not increment by 1. 总计不增加1。

[edit] [编辑]

I did how @mahaidery told me to, and my current php is 我做了@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++;
    }
    }

It shows a lot of Notice: Undefined variable: key 它显示了很多Notice: Undefined variable: key

You are doing wrong in PHP. 你在PHP中做错了。 This is how you can do it. 这就是你如何做到的。

<?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
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM