Jump to content

Checkboxes in HTML forms


jwermont

Recommended Posts

I have an HTML form with a checkbox type input. When I analyze the input on the server side, I notice that the checkbox element is only sent back to the server if it is checked. Otherwise, that element is not returned to the server at all.I'm using the POST method in the form, and the input element looks like this:

<input type=checkbox name=gifted>

If the user checks the checkbox, the "gifted" element will contain the value "on".If the user does not check it, there is no "gifted" element in the data sent to the server.Is there a way to force the POST to send a value of "0" (or something like that) when a checkbox is not checked by the user? All the other input types send something back, if only a null value in the case of no input.The reason I'm asking is that, in the server-side code, I try to check for the value of the checkbox, but if that element is simply missing, the test for the value doesn't work properly. (Makes sense - hard to test the value of something that doesn't exist!)Alternatively (if there's no way to force the POST to send a null value for an unchecked checkbox), is there a way to test the input buffer on the server side to see whether that element is there?Thanks,Joyce

Link to comment
Share on other sites

and with Java Servlets/JSP you can check the existence of a parameter (doesn't matter or GET or POST) with

...if (request.getParameter("paramname")!=null) then...

in your case paramname is the name of the checkbox

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...