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

后端 未结 12 907
盖世英雄少女心
盖世英雄少女心 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
    }
    

提交回复
热议问题