How get value for unchecked checkbox in checkbox elements when form posted?

前端 未结 11 2385
独厮守ぢ
独厮守ぢ 2020-11-27 17:13

I have a form like below :

相关标签:
11条回答
  • 2020-11-27 17:36

    First way - hidden fields (disadvantage: the user can manipulate the value of the field (but one can manipulate the value of the checkbox too, so it's not really a problem, if you only expect 1 or 0))

    <form action="" method="post">
    <input type="hidden" name="status_1" value="0" />
    <input type="checkbox" id="status_1" name="status_1" value="1" />
    <input type="hidden" name="status_2" value="0" />
    <input type="checkbox" id="status_2" name="status_2" value="1" />
    <input type="hidden" name="status_3" value="0" />
    <input type="checkbox" id="status_3" name="status_3" value="1" />
    <input type="submit" />
    </form>
    <?php
    var_dump($_POST);
    /*
     * checking only the second box outputs:
     * 
     * array (size=3)
      'status_1' => string '0' (length=1)
      'status_2' => string '1' (length=1)
      'status_3' => string '0' (length=1)
     */
    

    Second way - to assign default value for non-set indexes:

    <form action="" method="post">
    <input type="checkbox" id="status_1" name="status_1" value="1" />
    <input type="checkbox" id="status_2" name="status_2" value="1" />
    <input type="checkbox" id="status_3" name="status_3" value="1" />
    <input type="submit" />
    </form>
    <?php
    for($i = 1; $i<=count($_POST); $i++) {
        $_POST["status_$i"] = isset($_POST["status_$i"]) ? $_POST["status_$i"] : 0;
    }
    var_dump($_POST);
    
    /**
     * Here we will be checking only the third checkbox:
     * 
     * array (size=3)
      'status_3' => string '1' (length=1)
      'status_1' => int 0
      'status_2' => int 0
     */
    
    0 讨论(0)
  • 2020-11-27 17:41

    try below code

        $myresult = array();
    
        if(!isset($_POST['status_1'])){ 
            $myresult['status_1'] = 0;
        }
        if(!isset($_POST['status_2'])){ 
            $myresult['status_2'] = 0;
        }
        if(!isset($_POST['status_3'])){ 
            $myresult['status_3'] = 0;
        }
    
        echo "<pre>";
        print_r($myresult);
        echo "</pre>";
        exit;
    
    0 讨论(0)
  • Thanks all. Thank to @RoyalBg give me solution. Like this :

    <input type="hidden" name="status_1" value="0" />
    <input type="checkbox" id="status_1" name="status_1" value="1" /> Check 1 <br />
    <input type="hidden" name="status_2" value="0" /> 
    <input type="checkbox" id="status_2" name="status_2" value="1" /> Check 2 <br />
    <input type="hidden" name="status_3" value="0" />
    <input type="checkbox" id="status_3" name="status_3" value="1" /> Check 3 <br />
    

    It's work perfectly.. :)

    0 讨论(0)
  • 2020-11-27 17:46
    <!--html code-->                                                                  
    <input type="checkbox" name="correct" value="1">Option 1
    <input type="checkbox" name="correct" value="2">Option 2
    <input type="checkbox" name="correct" value="3">Option 3
    <input type="checkbox" name="correct" value="4">Option 4                                   
    
    //php code in function called on form submit       
      public function addOptions(Request $request)
       {
        $option = array('1' => 0,'2'=>0,'3'=>0,'4'=>0 );
        $option[$request->correct] = 1;
        return $option;
       }
    
    0 讨论(0)
  • 2020-11-27 17:47

    Try this. If the checkbox is not checked, then the hidden field with the same name will be passed instead.

    <form action="" method="post">
      <input type="hidden" id="hidden_status_1" name="status_1" value="0" />
      <input type="checkbox" id="status_1" name="status_1" value="1" />
      <input type="hidden" id="hidden_status_2" name="status_2" value="0" />
      <input type="checkbox" id="status_2" name="status_2" value="1" />
      <input type="hidden" id="hidden_status_3" name="status_3" value="0" />
      <input type="checkbox" id="status_3" name="status_3" value="1" />
    </form>
    
    0 讨论(0)
提交回复
热议问题