• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

handling a parameter with multiple values

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a form which can have multiple text fields with same name and it goes like this:

<td><INPUT TYPE="text" NAME="quantity" VALUE="" SIZE="10" MAXLENGTH="150"></td>
<td><INPUT TYPE="text" NAME="partNumber" VALUE="" SIZE="20" MAXLENGTH="150"></td>
<td><TEXTAREA NAME="partDescription" ROWS=5 COLS=25></TEXTAREA></td>
<td><TEXTAREA NAME="reasonForReturn" ROWS=5 COLS=25></TEXTAREA></td>

the above row can be repeated multiple times using javascript

i need to pass the parameter names to a servlet and do some processing.

if i do the following only one set of values is available to me

String qty[] = request.getParameterValues("quantity");
String number[] = request.getParameterValues("partNumber");
String partDesc[] = request.getParameterValues("partDescription");
String reasonReturn[] = request.getParameterValues("reasonForReturn");

can someone plese guide me how i can handle this.

Thanks
[ July 02, 2008: Message edited by: Bear Bibeault ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"benjamin",
Please check your private messages.
-DOM
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi benjamin , welcome to Javaranch,
First things first, your name does not follow the Javaranch naming policy
please change it here.

And regarding your question, its obvious that you will get the last input values, if you loop the html code many times.


Hope this helps .
[ July 02, 2008: Message edited by: Amit Ghorpade ]
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure that java script is adding the new text boxes in correct form
[ July 02, 2008: Message edited by: sudhir nim ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I handle this kind of thing by creating the field names on the fly and adding a number. So you would have fields named pD1 with matching reason1 etc.. Without that you can never be sure which part description field goes with which reason field - the order of parameters in the request is NOT guaranteed.

Bill
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by muktesh benjamin:
if i do the following only one set of values is available to me


Please explain what you mean by this. If there are multiple instances of a named input in the same form, you should be getting an array of the values.
 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is what i expected.
but its not working, i just get the first set of data.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"it's not working" isn't very useful.

Are you sure that the multiple input elements are being placed into the same form? Are you sure that the HTML constructs that you are creating are valid? Have you inspected the POST body to see if it contains all the values?

More info on how this is being handled on the client side may be required.
 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ill try to elaborate

Following is the chunk of form which will get repeated if there is more than one set of rows.
My javascript is giving the same name to the fields.
example if a i have 3 rows the field "quantity" should have three values and so on.

<td><INPUT TYPE = "hidden" name="returnItemId"/></td>
<td><input type="checkbox" name="item" /></td>
<td><INPUT TYPE="text" NAME="quantity" VALUE="" SIZE="4" MAXLENGTH="4"/></td>
<td><INPUT TYPE="text" NAME="partNumber" VALUE="" SIZE="18" MAXLENGTH="18"/></td>
<td><TEXTAREA NAME="partDescription" ROWS=3 COLS=40></TEXTAREA></td>
<td><TEXTAREA NAME="reasonForReturn" ROWS=3 COLS=40></TEXTAREA></td>

In my servlet i am trying to get the parameters like
String quantity[] = request.getParameterValues("quantity");
when i try to get all the values for "quantity" in a for loop like

for (int i = 0; i < quantity.length; i++) {
//do something
}

I just get one set of values.

I am lost right now. I am trying to add an id field to my table and see if i can handle it.

Thanks
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the multiple input elements are being placed into the same form? Are you sure that the HTML constructs that you are creating are valid? Have you inspected the POST body to see if it contains all the values?
 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the multiple input elements are being placed into the same form? YES

Are you sure that the HTML constructs that you are creating are valid?YES


Have you inspected the POST body to see if it contains all the values? YES
 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i am doing some processing with iText in the servlet i suspect that i need to look into that aspect.
Thanks for your time anyways.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic