• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Input Checkbox Always 0

Robust

Well-known member
#1
Code:
<input type="checkbox" name="invisible" value="0" id="ctrl_invisible" />
So, this the above code is a checkbox option I have. I ran a Zend_Debug::dump in the controller. It's always returning 0 as the value, even if the checkbox is checked.

I use XenForo_Input to filter this as UINT (I have tried BOOLEAN already). It is meant to be stored into the database as TINYINT(1), however I have also tried as an unsigned integer.

Am I doing something wrong? I looked at XF's examples, and it seems like XF doesn't do checkboxes with default value 0, only checkboxes with default value 1. For default value 0 it seems to do radio options.
 

Chris D

XenForo developer
Staff member
#2
The value submitted to a form from a checkbox element is always 0 when it is unchecked. When it is checked it is whatever the value attribute is.

So in your example above as you have set the checkbox value attribute to 0, when it is unchecked you will receive a value of 0, but also when it is checked you will still receive a value of 0.

The majority of the time, for a checkbox that represents a boolean choice, the value attribute should be 1 (if it is unchecked it will automatically be 0).
 

Robust

Well-known member
#3
The value submitted to a form from a checkbox element is always 0 when it is unchecked. When it is checked it is whatever the value attribute is.

So in your example above as you have set the checkbox value attribute to 0, when it is unchecked you will receive a value of 0, but also when it is checked you will still receive a value of 0.

The majority of the time, for a checkbox that represents a boolean choice, the value attribute should be 1 (if it is unchecked it will automatically be 0).
You know, at this rate you're going to be in the 'Thank you' section of every single one of my add-ons. Thanks once again, Chris.