Value=TRUE for Checkbox

Could someone help me get a better understanding of what this code really says…


	<th class='colSelect' />
		<input name='selectAll' type='checkbox' value='TRUE' />
	</th>

I think it means that IF the Checkbox is checked, and IF it is submitted on a Form, THEN it will have a value of “TRUE”, which for example, might be what is stored in the database?

(When I first read this last night - for some old code I forgot about - I thought it was saying, “This Checkbox is pre-selected/pre-checked with a value of ‘TRUE’…” which is not what I want?!)

Comments?

Thanks,

Debbie

I thought it was saying, "This Checkbox is pre-selected/pre-checked

In order to have that, the code you posted would also need to have
checked
or
checked=“checked”

to mean pre-selected. The “checked” attribute when hand-coded into the HTML is considered part of the “default” values of the form (what you would have if you made changes to the form, but then hit a input type=“reset” button… also Javascript, or at least many JS libraries, make a distinction between default checked/unchecked states and states set by the user).

Okay, but you missed the main question which is trying to understand what value=‘TRUE’ means and does… :wink:

Debbie

The VALUE attribute, required for radio buttons and checkboxes, unsurprisingly gives the value (chosen by the author) of the control when it is checked. The boolean CHECKED attribute specifies that the control is initially checked.

Each checkbox stands for something—an option—and you place something in the value attribute to indicate what that option is (for the sake of PHP). So when you get the results of the form, you’ll know which option was chosen via the value that is returned. Presumably there would also be a label of “True” associated with this input, or something similar, for the sake of the user, as in:

The end is nigh!

  • True
  • False

So I had it right in my original post.

Thanks,

Debbie

s/required/optional/

When this form is submitted, selectAll=“TRUE”. Do note this is the STRING “TRUE” and not a boolean TRUE ( tho the string will eval to TRUE).
For example if the value was “FALSE” selectAll=“FALSE”, but since it’s the STRING “false” and not the boolean FALSE, it too will eval as TRUE!!!

for this reason its best to use a 1 (true) or a 0 (false) in this kind of situation.

Yes, value is optional except when the type attribute has the value “radio” or “checkbox”.

There’s also an error in the way the input is NOT wrapped inside a <th> tag. If the <th/> preceding it is self closing and you are serving the page as XHTML then the page should not display because there is no opening tag to correspond to the </th> following the input. If served as HTML then the / is invalid anyway - but will be ignored.

[citation needed]

Strangely: http://www.w3.org/TR/html401/interact/forms.html#adef-value-INPUT

Thanks.

The value attribute is always optional in the current HTML spec. Radio buttons and checkboxes have the default value “on”. http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#dom-input-value-default-on

While the attribute is optional, it’s important the server knows what the client is trying to convey.

Radio buttons and check boxes are included in the submitted request if checked. With checkboxes, as long as their name is unique, there isn’t a problem with them having no value as long as the server side code is written to deal with it. 99% of the time that isn’t the case - the server side code will check to see if the value is logically true. The default value of any input element is empty string in PHP, which is logically false. So, we have a problem.

Radio buttons without values are worse. In order to get the radio behavior all the radio buttons of a set should share a name. Unless they have different values there’s no way to ascertain which one was checked from the server side.

Just because code is valid doesn’t mean it will be functional, or even clear to the maintenance programmer that comes after you.