Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #24272

Re: Why checkbox field is not detected when the form is submitted?

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: Why checkbox field is not detected when the form is submitted?
Date 2014-05-20 07:56 +0300
Organization A noiseless patient Spider
Message-ID <llena0$e0i$1@dont-email.me> (permalink)
References <149e228b-45da-4203-a4b9-6a01b089f257@googlegroups.com>

Show all headers | View raw


2014-05-19 22:41, justaguy wrote:

> The following fiddle js code for ON and OFF switch is very cool,
> however, the checkbox field is not included the FORM when it is
> submited, how come?

Because it has no name attribute. Oh, and because the form can never be 
submitted when using the code in the jsfiddle: there is no submit button 
and not even any <form> element.

> Please note, I've added name and value attributes to this checkbox.

Not in the jsfiddle.

> When the form is submitted and when checkbox is set to OFF then the
> field and its value shows up in the FORM submission, hmm...
>
> http://jsfiddle.net/boweruk/DMfvx/7/

I don't think they do. If you add a `name` attribute to the `input` 
element, and if you add a <form> element and an input button, then a 
name=value pair is included in the form data upon submission when the 
checkbox is checked, not otherwise. The CSS code does not affect this; 
this is just how HTML elements are handled.

The code does not contain any JavaScript, so I'm assuming you are asking 
how to fix the problem with JavaScript. The short answer is that you 
could add a hidden field into the form and set its value according to 
the checkbox setting. But this would be pointless in most contexts. 
Checkboxes are supposed to work so that name=value is included when the 
checkbox is on, not otherwise. Form handling, client-side and 
server-side, should be written to work with this (on the server, just 
check for the existence of a field with the name; you can check the 
value too, but that would be just data integrity check) rather than work 
around it.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why checkbox field is not detected when the form is submitted? justaguy <lichunshen84@gmail.com> - 2014-05-19 12:41 -0700
  Re: Why checkbox field is not detected when the form is submitted? justaguy <lichunshen84@gmail.com> - 2014-05-19 13:01 -0700
    Re: Why checkbox field is not detected when the form is submitted? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-05-19 23:55 +0200
  Re: Why checkbox field is not detected when the form is submitted? dhtml <dhtmlkitchen@gmail.com> - 2014-05-19 16:42 -0700
  Re: Why checkbox field is not detected when the form is submitted? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2014-05-20 07:56 +0300
  Re: Why checkbox field is not detected when the form is submitted? Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-20 10:19 +0000

csiph-web