How do I handle FORMs with multiple form elements (e.g. radio buttons) using the same name?

Alex Chaffee

For radio buttons, the HTML spec assumes that a given group of buttons will have the same NAME and different VALUEs; the browser makes sure that only one button per group name will be selected (at most). So you can just call request.getParameter("groupname").

<input type="radio" name="topping" value="cheese" checked>Cheese
<input type="radio" name="topping" value="pepperoni">Pepperoni
<input type="radio" name="topping" value="anchovies">Anchovies
If the user selects "Pepperoni" then request.getParameter("topping") will return the string "pepperoni".

For lists using the <select multiple> FORM tag, multiple values can be returned for the same parameter name. When that can happen, use request.getParameterValues("param") which returns a String[] you can iterate through.

It's bad form (so to speak), but you can also duplicate other element types, like
Name 1: <input type="text" name="name" value="Dick">
Name 2: <input type="text" name="name" value="Jane">
These also get returned in an array by request.getParameterValues().

See http://hotwired.lycos.com/webmonkey/99/30/index4a.html for a good tutorial on HTML FORMs.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact