Solved

<select ... <option... handling on the server side

Posted on 2002-07-24
4
164 Views
Last Modified: 2011-10-03
Hi!

I have a couple of dynamically created <select size="10" multiple> elements (http://www.w3.org/TR/REC-html32#select). I'm wondering if it is possible to retrieve several <option> values from one <select> on to the server side?

I tried to list the parameters received by the servlet with the following snippet:

    Enumeration e   = request.getParameterNames();
    String s        = "";
    while ( e.hasMoreElements() ) {
      s = (String)e.nextElement();
      System.out.println( "Param: <" + s + "> Value: <" + (String)request.getParameter(s) + ">" );
    }


But I received only one value from each <select> (both had multiple options selected):

Param: <listboxFooBar> Value: <foo>
Param: <listboxBarFoo> Value: <bar>
Param: <Submit> Value: <Tallenna>

I will increase the points for this question to 300 if a solution is found. If no solution is found then the first one to answer "it can't be done" will get the points :D


Regards,

Marko
0
Comment
Question by:_marko_
  • 3
4 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 7175270
it can't be done.

(actually, I think it can ... but just in case it can't, I wanted to be first:)

stand by, I'll look into it ...
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 300 total points
ID: 7175339
use request.getParameterValues instead ... which returns a String[] ... for example:



<HTML>
<BODY>
<FORM name='myform' method='post'>
 <SELECT name='myselect' multiple>
  <OPTION value='0'>Zero</option>
  <OPTION value='1'>One</option>
  <OPTION value='2'>Two</option>
  <OPTION value='3'>Three</option>
  <OPTION value='4'>Four</option>
  <OPTION value='5'>Five</option>
  <OPTION value='6'>Six</option>
  <OPTION value='7'>Seven</option>
 </select>
 <INPUT type='submit'>
</form>
<P>
<%
  String[] sa = request.getParameterValues("myselect");

  for ( int i=0; i<sa.length; i++ )
  {
    %><%= sa[i] + ", " %><%
  }
%>
</body>
</html>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7175351
forgot to check for nulls ...


  if ( sa==null )
  {
     %><%=request.getParameter("myselect")%><%
  }
  else for ( int i=0; i<sa.length; i++ )
  {
     %><%= sa[i] + ", " %><%
  }
0
 
LVL 2

Author Comment

by:_marko_
ID: 7176475
Hello knightEknight!

Yes :-) That was exactly what I was looking for. Sorry for the delay (was out of the office).


Thanks!


Marko
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question