Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form submit checkbox sets value to "on" rather than "true"

Hi I have an html form which I am submitting via the click event on a button. The event fires $("#myform").submit(); the problem is that there is a checkbox on the form and in firebug under the posted params it shows "mycheckbox1 on" rather then the expected "mycheckbox1 true".

when submitting a form via ajax I can set the data that is posted no problem but this form has a file upload which requires one of the various hacks to make it work. The one I'm using ultimately calls submit. but perhaps that is not relevant.

In any case when the data arrives at the server, the server doesn't see the value "on" as a bool and thus ignores it.

Any insight would be greatly appreciated.

like image 594
Raif Avatar asked Feb 07 '12 23:02

Raif


People also ask

How can we set a value to a checkbox field so that it sets to true if not checked?

1) You (un)selected the checkbox on the first page and submitted the form. 2) Your building the second form and you setting the value="" true/false depending on if the previous one was checked. 3) You want the checkbox to reflect if it was checked or not before.

How do you set a checkbox to false value?

val( checkbox[0]. checked ? "true" : "false" ); This will set the value of the checkbox to "true" or "false" ( value property is a string) , depending whether it's unchecked or checked .

Does checkbox return true or false?

The Input Checkbox defaultChecked property in HTML is used to return the default value of checked attribute. It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.


1 Answers

A checked checkbox simply sends its value attribute. An unchecked checkbox doesn't send itself in the form.

Therefore, a simple test if the checkbox's name attribute was posted can determine if it was checked or not.

like image 83
alex Avatar answered Sep 23 '22 17:09

alex