wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes always get the value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "always get the value "20" no matter which checkbox I clicked" Watch "always get the value "20" no matter which checkbox I clicked" New topic
Author

always get the value "20" no matter which checkbox I clicked

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
After clicking the checkbox, I like to get the value of the checkbox submitted.
Why the following page always get the value "20" for checkboxChoice and checkboxvalue no matter which checkbox I clicked?
Please help.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Please UseCodeTags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
What happens if you use onchange?

Eric
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41148
    
  45
What does the "submit" method do? I'm not surprised that a form that contains multiple fields of the same name doesn't work as expected.


Ping & DNS - my free Android networking tools app
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
I like to have an array of checkboxes and clicking each checkbox will submit its value (either a normal or hidden value) and the JSP will find out which checkbox(es) is/are clicked.
Please review the following html. I replaced "onclick" with "onchange" but clicking the checkbox 19 still does not get the value 19.
What will the JSP (or PHP) look like?
Please help.
Thanks.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
If it is set to default checked, it will send up the value to the server.

If more than one thing is checked and they have the same name, you should have an array of values that are selected.

Eric
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41148
    
  45
Start by changing the form so that all input elements have unique names.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

I'll just point out that it is perfectly valid to have multiple elements with the same name -- in fact, it's a must for radio groups -- as long as you want the values to be delivered as an array to the servlet.

I suspect that you're just not dealing with the data values correctly on the server.
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
Bear Bibeault wrote:I'll just point out that it is perfectly valid to have multiple elements with the same name -- in fact, it's a must for radio groups -- as long as you want the values to be delivered as an array to the servlet.

I suspect that you're just not dealing with the data values correctly on the server.


My JSP is


The JSP output is as follows - i.e. not receiving the input.
checkboxChoicenull
checkboxvalue null
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41148
    
  45
That code does not make use of the field "checkboxvalue" - that looks like a bug.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

You put the Java code in a JSP?
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
Ulf Dittmer wrote:That code does not make use of the field "checkboxvalue" - that looks like a bug.


Following your advice, I change the code to:


However, clicking the checkbox still get null value.
albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
Bear Bibeault wrote:You put the Java code in a JSP?


To clarify, I put the Java code in a java servlet to handle the JSP.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Whew!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41148
    
  45
You could perform a quick check of what actually gets sent over the wire by using a tool like the LiveHttpHeaders extension for Firefox.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you have multiple fields with the same name and want to retrieve them as an array, you need to use HttpServletRequest.getParameterValues.

If you use getParameter you will only get the first value in the array.

See:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getParameterValues%28java.lang.String%29


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ayoma Wijethunga
Ranch Hand

Joined: Mar 07, 2010
Posts: 38

Ben Souther wrote:If you have multiple fields with the same name and want to retrieve them as an array, you need to use HttpServletRequest.getParameterValues.


That's it ..


Your form is passing this kind of data to the taglocation.htm.


checkboxChoice= (Because hidden field is empty)
checkboxChoice=20 (Because 20 checkbox was checked)
checkboxChoice= (Because hidden field is empty)
checkboxChoice= (Because hidden field is empty)
checkboxChoice=18 (Because 18 checkbox was checked)

checkboxChoice=19 will not be sent because 19 checkbox was NOT checked in this test.

So Ben Souther suggestion is all you need to make this work. getParameterValues(java.lang.String) method will return an array with all the parameter values, passed with the name of string you pass. So what you have to do is.



Also it seems like the hidden fields are messing your work. So it'll be easy if you could change the names of hidden fields to something else.

Regards.


AyomaOnline - CodeGreed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: always get the value "20" no matter which checkbox I clicked
 
Similar Threads
Pressing the enter key receive extra invalid data
Space not releasing on toggle of divs
How to update a image using servlet?
jQuery not working on page
Doubts in jsp