Do checkbox inputs only post data if they're checked?

后端 未结 12 753
盖世英雄少女心
盖世英雄少女心 2020-11-22 08:04

Is it standard behaviour for browsers to only send the checkbox input value data if it is checked upon form submission?

And if no value data is supplied, is the defa

相关标签:
12条回答
  • 2020-11-22 08:30

    Is it standard behaviour for browsers to only send the checkbox input value data if it is checked upon form submission?

    Yes, because otherwise there'd be no solid way of determining if the checkbox was actually checked or not (if it changed the value, the case may exist when your desired value if it were checked would be the same as the one that it was swapped to).

    And if no value data is supplied, is the default value always "on"?

    Other answers confirm that "on" is the default. However, if you are not interested in the value, just use:

    if (isset($_POST['the_checkbox'])){
        // name="the_checkbox" is checked
    }
    
    0 讨论(0)
  • 2020-11-22 08:33

    Just like ASP.NET variant, except put the hidden input with the same name before the actual checkbox (of the same name). Only last values will be sent. This way if a box is checked then its name and value "on" is sent, whereas if it's unchecked then the name of the corresponding hidden input and whatever value you might like to give it will be sent. In the end you will get the $_POST array to read, with all checked and unchecked elements in it, "on" and "false" values, no duplicate keys. Easy to process in PHP.

    0 讨论(0)
  • 2020-11-22 08:34

    I have a page (form) that dynamically generates checkbox so these answers have been a great help. My solution is very similar to many here but I can't help thinking it is easier to implement.

    First I put a hidden input box in line with my checkbox , i.e.

     <td><input class = "chkhide" type="hidden" name="delete_milestone[]" value="off"/><input type="checkbox" name="delete_milestone[]"   class="chk_milestone" ></td>
    

    Now if all the checkboxes are un-selected then values returned by the hidden field will all be off.

    For example, here with five dynamically inserted checkboxes, the form POSTS the following values:

      'delete_milestone' => 
    array (size=7)
      0 => string 'off' (length=3)
      1 => string 'off' (length=3)
      2 => string 'off' (length=3)
      3 => string 'on' (length=2)
      4 => string 'off' (length=3)
      5 => string 'on' (length=2)
      6 => string 'off' (length=3)
    

    This shows that only the 3rd and 4th checkboxes are on or checked.

    In essence the dummy or hidden input field just indicates that everything is off unless there is an "on" below the off index, which then gives you the index you need without a single line of client side code.

    .

    0 讨论(0)
  • 2020-11-22 08:36

    None of the above answers satisfied me. I found the best solution is to include a hidden input before each checkbox input with the same name.

    <input type="hidden" name="foo[]" value="off"/>

    <input type="checkbox" name="foo[]"/>

    Then on the server side, using a little algorithm you can get something more like HTML should provide.

    function checkboxHack(array $checkbox_input): array
    {
        $foo = [];
        foreach($checkbox_input as $value) {
            if($value === 'on') {
                array_pop($foo);
            }
            $foo[] = $value;
        }
        return $foo;
    }
    

    This will be the raw input

    array (
        0 => 'off',
        1 => 'on',
        2 => 'off',
        3 => 'off',
        4 => 'on',
        5 => 'off',
        6 => 'on',
    ),
    

    And the function will return

    array (
        0 => 'on',
        1 => 'off',
        2 => 'on',
        3 => 'on',
    )  
    
    0 讨论(0)
  • 2020-11-22 08:38

    Yes, standard behaviour is the value is only sent if the checkbox is checked. This typically means you need to have a way of remembering what checkboxes you are expecting on the server side since not all the data comes back from the form.

    The default value is always "on", this should be consistent across browsers.

    This is covered in the W3C HTML 4 recommendation:

    Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.

    0 讨论(0)
  • 2020-11-22 08:40

    input type="hidden" name="is_main" value="0"

    input type="checkbox" name="is_main" value="1"

    so you can control like this as I did in the application. if it checks then send value 1 otherwise 0

    0 讨论(0)
提交回复
热议问题