• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

checkbox values on form submit

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following snipit of html in a form:

It's dynamic so the user can add new rows...it's just prompting them for the name of a language they speak and the check box is to indicate if they write in that language as well. In the servlet I use getParameterValues() to get the verbalLanguage and written fields. If they submit 3 different languages and the first and the third are written I get something like this:
verbalLanguage={"English","Spanish","French"}
written = {"written","written"}

I know why I'm getting this, (it's because the checkbox fields are only submitted when they're checked) but what can I do so the values of written match up with the values of verbalLanguage? Ideally I'd get something like:
written = {"written",null,"written"}

I hope I've explained this clearly enough....any ideas?

-Jeff
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't count on the order of http parameters coming from a browser.

If it were me, I would work the name of the language into the value attribute of the checkbox.
<input type="checkbox" name="written" value="french_written" />

Then, match them up wherever you parse the params.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. A suggestion: in your HTML always quote attribute values as if you were writing XHTML. It will save your butt one day.
 
Jeff Strike
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Souther wrote:You can't count on the order of http parameters coming from a browser.


Is that part of the specification or an observation on your part? Based on what I've seen the parameters have always come over in the same order they're on the page. (IE, FF and Chrome are the ones I've noticed this with)

Bear Bibeault wrote:P.S. A suggestion: in your HTML always quote attribute values as if you were writing XHTML. It will save your butt one day.


Typically I do...that was just for the sake of brevity, thanks for pointing that out though.

-Jeff

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's always browser-dependent, may change, the DOM (think hidden properties) may have an impact, etc. Do not rely on the order.

If you did, all you'd need to do is set a hidden value with the initial value of the checkbox to account for the "unchecked" options.

If you didn't (the correct course of action), maybe use some sort of index along with a known name. Since the languages aren't known, I'd recommend against changing the ID on-the-fly to include the name of the language.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic