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.
Inputs in a form cannot have the same name unless they are an array. There’s also no connection between your checkboxes and hidden inputs. When a form is submitted to its target, it’s simply a collection of independent name/value pairs.
You should try creating only ONE hidden input called “checkboxvalue”, and setting that input’s value in your submit() function before the form is submitted.
Unless there’s a purpose for having the hidden checkboxvalue, it tends to work best when you have just the checkboxes themself. That way the values of the selected checkboxes will be the only ones to be submitted.
With checkboxes it actually works best if you have a hidden field first and then the checkbox and give then both the same name (which is not used for anything else). That way if the checkbox is checked then the checkbox value gets passed and if it isn’t the hidden field value gets passed.
Relying on the browser to choose among multiple form fields with the same name dependent on the order they appear in the form seems like relying on a specific browser implementation
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.
What will the JSP (or PHP) look like?
I haven’t been able to find a browser where it doesn’t work that way. In every case where multiple fields with the same name exist in a form it is always the last one referenced in the form that is the one that gets passed when the form is submitted. I don’t know whether this is a part of a standard somewhere or whether all the browsers just implemented it that way by accident but it does work correctly in the several dozen different browsers I have tried it with.
Here’s some example code from W3C that shows several checkboxes with the same name. From their usage, it appears that the W3C expects them to all be submittable.