• Post Reply Bookmark Topic Watch Topic
  • New Topic

checkbox values on form submit

 
Jeff Strike
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!