• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need help with Checkboxes

 
Thomas Knight
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
I searched the forum for info on checkboxes and I did not find anything specific to my problem. I have 2 files, a HTML and a JSP. The HTML calls the JSP and passes it parameters. I grab these parameters in my JSP and make use of it using request.getParameter("Name of form element").
On my HTML page I have 2 checkboxes one for "apple" and one for "orange".
Therefore my HTML code looks something like this....

I am trying to see which of the checkboxes are checked and do a particular action. I know that when a check box is checked, you get a value of "on" and when it is unchecked it is null. But when I test for their presence I get into problems. Here is what my JSP code kind of looks like...

None of these blocks of code executing and so I am getting an error saying that z is an unreferenced symbol. I have also tried the following in place of the above code with the same results..

Any clue why this is happening? I did a out.println of the 2 checkbox values and I get null and on properly, but when I use that value to do a comparision it doesn't work.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
A few things from your code:
1) If you declare the variable z (by saying String z) within an if statement block, it is only valid within that block. You need to declare it before the if statements and just assign it in the if statement. That way you can access z later in the program.
2) a.equalsIgnoreCase("null") == true will not work. You can use (a == null) instead. Note that null is a value like undefined, while "null" is a string that just happens to contain the letters "n", "u", "l" and "l".
3) (null = request.getParameter("orange") is not working because = is an assignment operator while == is a comparison operator.
 
Thomas Knight
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That was the explanation I was waiting for.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use this code instead:

you can check on

which is slightly shorter and more readable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!