views:

586

answers:

3

Hi all, I've got a single checkbox which i'd like the unchecked value to be 0 and the checked value to be 1, but when the post goes through, it always shows as 1 whether the box is checked or not..

Here's the checkbox:

<input name="stock[]" type="checkbox" id="stock[]"> value="1" />

here's what it spits out regardless of whether it's checked or not.. (there are multiple "stock" checkboxes..

[stock] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 1
            [5] => 1
        )

I can't seem to get it working.. :S Any ideas? :)

+3  A: 

It seems like you have a extra > in your Checkbox HTML. Value is not used in case of checkboxes.

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

because you have value="1", the form will always submit the value regardless of whether checked or not. So remove the value attribute will help.

And to preset the checkbox to be checked, use the checked attribute instead. Example:

 <input name="stock[]" type="checkbox" checked="checked" />
thephpdeveloper
and this is why you should always validate your markup to QA yourself.
meder
The value attribute is not only used, it's REQUIRED for both checkboxes and radio buttons.
Martha
@Martha - not true. I am using XHTML 1.0 Transitional and it is not required. by putting even a `value=""`, the field is considered set and will be sent in the request.
thephpdeveloper
Mauris, I'm not seeing any support for your assertion in any of the specs. This would be a rather fundamental difference between XHTML and every other version of HTML, and would severely break any forms that were written in one version and ported to the other, so I would expect it to be well-documented in the various lists of "differences between html and xhtml".
Martha
Martha is correct.The value attribute does not define if a checkbox is checked or not. What it does is define *which value* it would have *if it is checked*.Checked or not checked is defined by the "checked" attribute.
Carlos Lima
For reference, check this: http://www.w3.org/TR/html4/interact/forms.html#h-17.4 Where it states: [ value = cdata [CA]This attribute specifies the initial value of the control. It is optional except when the type attribute has the value "radio" or "checkbox". ] and [ checked [CI]When the type attribute has the value "radio" or "checkbox", this boolean attribute specifies that the button is on. User agents must ignore this attribute for other control types. ]
Carlos Lima
A: 

I'm not sure you can/should name an input element "stock[]" - try getting rid of the [] and re-test.

Traveling Tech Guy
it's fine with stock[], because in PHP it will be parsed into an array if you have multiple fields using the same name with brackets at the back.
thephpdeveloper
That's a common technique for `PHP` forms, though I think it's technically incorrect as other languages' internal CGI processors handle it fine without the `[]`.
meder
+1  A: 

(Obviously, you need to start by removing that extraneous > sign.)

Checkboxes and radio buttons must have a value attribute. If they're checked, that value is sent when the form is submitted; if they're not checked, no value is submitted. Thus, you can't directly do the "0 if unchecked, 1 if checked" thing. What you can do instead is use the value attribute to detect which checkboxes were checked.

<input type="checkbox" name="stock[]" value="1" id="stock1" />
<input type="checkbox" name="stock[]" value="2" id="stock2" />
<input type="checkbox" name="stock[]" value="3" id="stock3" />

If the 1st and 3rd boxes are checked, but the 2nd is not, then when you look at the value of the stock[] array in your code, you'll find that it contains 1 and 3, but not 2.

Edit: I just confirmed that the reason it looked like all of your checkboxes had values submitted was that you failed to give the form any way to differentiate among them. If you have multiple checkboxes with not only the same name but the same value, then your form results will look something like "stock[] = 1, stock[] = 1, stock[] = 1". PHP will then interpret this as stock[1] = 1, stock[2] = 1, etc., but the point is that stock[2] might actually be the fourth checkbox - neither the form nor php has any way to tell where each value came from.

So bottomline is, you can either use the same name for a series of checkboxes, OR you can use the same value, but not both at once: if you use the same name, you have to use the value to differentiate among the controls; and vice versa, if you want to use the same value, you need to use different names.

By the way, if you don't specify the value of a checkbox, then most browsers will supply a default value of their own - for example, IE and Firefox use "on" (or "ON" in some versions). But depending on such defaults is always a bad idea.

Martha