检测未选中的复选框php

时间:2012-08-24 19:15:04

标签: php

有没有办法检查一个复选框是否取消选中了PHP?我知道你可以在html中做一个隐藏的字段类型但是在提交表单时只用php怎么样?我试着下面没有运气。

if(!isset($_POST['server'])||$_POST['server']!="yes"){
        $_POST['server']     == "No";
}

4 个答案:

答案 0 :(得分:12)

如果未选中复选框,则不会发布。 if(!isset($_POST['checkboxname']))会做到这一点。

请注意,您至少应该提交某些内容,以便您首先知道该表单已提交。

if (isset($_POST['formWasSubmitted'])) {
    //form was submitted...let's DO this.

    if (!isset($_POST['checkboxname'])) {
        // checkbox was not checked...do something
    } else {
        // checkbox was checked. Rock on!
    }
}

答案 1 :(得分:1)

$checkedfeild = @$_POST["yourfeildname"];

if(isset($checkedfeild))
{
 //Code here
}
else
{
echo"Not checked";
}

答案 2 :(得分:1)

这是一个老问题,但对于那些寻找这个问题的人来说....

更好地处理马特的回答$_SERVER['REQUEST_METHOD']

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    //form was submitted...let's DO this.

    if (!isset($_POST['checkboxname'])) {
        // checkbox was not checked...do something
    } else {
        // checkbox was checked. Rock on!
    }
}

答案 3 :(得分:0)

尝试一下:

$checked = $_POST['notif'];
foreach($checked as $ch){
    if($ch == $i){
       /add element to checked set
       $array_checked[]=$ch;
    }
}
for($i=1;$i<6;$i++){
    if(in_array($i,$array_checked)){
        //do for checked
    }else{
        //do for unchecked
    }
}