4

有没有办法用php检查复选框是否未选中?我知道您可以在 html 中执行隐藏字段类型,但是在提交表单时仅使用 php 呢?我试过下面没有运气。

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

4 回答 4

17

如果未选中复选框,则不会发布。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!
    }
}
于 2012-08-24T19:17:46.077 回答
3

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

对马特的回答更好的方法是$_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!
    }
}
于 2015-07-30T15:47:46.357 回答
0
$checkedfeild = @$_POST["yourfeildname"];

if(isset($checkedfeild))
{
 //Code here
}
else
{
echo"Not checked";
}
于 2012-08-24T19:17:26.047 回答
-1

尝试这个:

$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
    }
}
于 2018-11-13T01:43:31.973 回答