detect unchecked checkbox php

前端 未结 4 1167
无人共我
无人共我 2020-12-11 09:38

Is there a way to check of a checkbox is unchecked with php? I know you can do a hidden field type in html but what about with just php when the form is submitted? I tried b

相关标签:
4条回答
  • 2020-12-11 10:17

    Try this:

    $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
        }
    }
    
    0 讨论(0)
  • 2020-12-11 10:32

    This is an old question, but for people looking for this....

    Better approach to Matt's answer is to use $_SERVER['REQUEST_METHOD'] to check if form was submitted:

    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!
        }
    }
    
    0 讨论(0)
  • 2020-12-11 10:33

    If a checkbox is not checked it will not be posted. if(!isset($_POST['checkboxname'])) will do the trick.

    Be aware, though, you should at least submit something so that you know the form was submitted in the first place.

    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!
        }
    }
    
    0 讨论(0)
  • 2020-12-11 10:36
    $checkedfeild = @$_POST["yourfeildname"];
    
    if(isset($checkedfeild))
    {
     //Code here
    }
    else
    {
    echo"Not checked";
    }
    
    0 讨论(0)
提交回复
热议问题