检测未选中的复选框php


detect unchecked checkbox php

有没有办法用php检查复选框是否被取消选中?我知道你可以在html中进行隐藏字段类型,但当表单提交时,只使用php怎么办?我试过了,但没有运气。

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

如果未选中复选框,则不会过帐。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!
    }
}

这是一个老问题,但对于寻找它的人来说。。。。

Matt答案的更好方法是使用$_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!
    }
}
$checkedfeild = @$_POST["yourfeildname"];
if(isset($checkedfeild))
{
 //Code here
}
else
{
echo"Not checked";
}

试试这个:

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