簡體   English   中英

PHP復選框,帶2個正確答案

[英]PHP checkbox with 2 correct answer

我正在做一個測驗,下面是一個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 for復選框無效。

即使我把它改成了

 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM