Cannot get checkbox value back to servlet

a_b
a_b used Ask the Experts™
on
This one is driving me nuts -

<form...............>
<input type='checkbox' name='chk'>
<input type='submit' name='test'>

At the servlet if I say request.getParameter("checkbox") orgetParameterValues("checkbox"), it always return null. I cannot figure out why? It's been a while since I did any programming in servlets/jsp so I am little rusty. Am I missing something basic?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
do you get null when the checkbox is checked ?

Commented:
If the checkbox is unchecked when the form is submitted, you won't find any value related to it in the request because only successful controls are submitted.

Commented:
On the other hand, given your example, even if the checkbox is checked you will get a null value because you call request.getParameter("checkbox") instead of request.getParameter("chk").
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Commented:
Good point ioanton.

Btw, you can add the "value" attribute to your checkbox, this is what you will get when your checkbox is checked and form submitted.
a_b
Top Expert 2009

Author

Commented:
Sorry guys, was a typo. The statement is equest.getParameter("chk"). I will try out the suggestion of placing  value by flob9 and paste the feedback.
a_b
Top Expert 2009

Author

Commented:
Tried adding the value also, but did not work.


<input type='checkbox' name='chk value='chk' id='chk''>
<input type='submit' name='test'>

request.getAttribute("chk")
request.getParameter("chk")

Nothing works, pretty sure I am missing something basic.
Database Administrator / Software Engineer
Commented:
         Hi!

Here is an JSP Form example on how to submit checkboxes
http://www.java2s.com/Code/Java/JSP/SubmittingCheckBoxes.htm

I believe the problem is in the mixing character ' and "  which should only be " 
Like this

<input type="checkbox" name="chk" value="chk" id="chk''>
<input type="submit" name="test">

Regards,
   Tomas Helgi
a_b
Top Expert 2009

Author

Commented:
@Tomas: I tried both way wasy with ' as well as ". Did not work.
Commented:
Please specify:
-is the form tag properly closed?
-are you sure the servlet is called?
-you may add to the form another control (say text field) to make sure the problem is related only to the checkbox controls
a_b
Top Expert 2009

Author

Commented:
@ioanton: Will try the last suggestion. Already checked the first 2.
Commented:
One last question: has your form enctype="multipart/form-data"?
when you are testing, are you checking the check box?

Also as thomas said above. Is your form tag opens and closes at the right place?
a_b
Top Expert 2009

Author

Commented:
The idea was scrapped. Decided to got for a popup instead to fit in additional requirement. Thanks guys.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial