?
Solved

how to submit multiple rows of data at one click

Posted on 2003-03-03
15
Medium Priority
?
235 Views
Last Modified: 2010-04-01
Dear experts,

I design a web page for user to submit data. There is an "add row" button which allows users to add as many row as they want. Therefor, they could submit multiple new data at one time.

The problem is how do we handle a case like this when we use "request.getParameter()" in action page to receive new value? There will be "unexpected" many paramters passed. How do we name them all in a easy way?


Thank.
0
Comment
Question by:changcy77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 3
15 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8058611
are you talking about checkboxes?

if you recieve multiple data in form field, its usually comma delimited right?

try using:
String[] values = request.getParameterValues("fieldname");

that should return an array of values passed in.

CJ
0
 

Author Comment

by:changcy77
ID: 8058823
Thanks.
In fact, the data submit page is a "table" which user can add as many rows as possible to. Once they fill up all the fields in the table, they could click "submit" button and send the new rows of data into database.

There are 5 filds in one row. Is there a better way than use "String[] values = request.getParameterValues("fieldname");" ?




0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8058890
5 fields or 5 values?

if the field values are delimited by something try java.util.StringTokenizer to break up the string into elements.

CJ
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:changcy77
ID: 8058995
there are 5 fields. for example:
the table looks like this:

field1 field2 field3 field4 field5
------ ------ ------ ------ -----
data1   data2  data3  data4  data5
data11  data22 data33 data44  data55
........

Are you saying that all the text input of the same field, should be named that same? And I will get a long string which concantenate all data from the same field? like data1,data11, data111....

Thanks.







0
 

Author Comment

by:changcy77
ID: 8059731
CJ,

I have tested your suggestion, but only the last row got passed instead of all the rows. (each row has 5 fields) Does anyone have a better idea how to pass multiple rows of data at once?


Thanks.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8059757
all the fields have the same name right?

<form action="">
<input type="text" value="field1"> <input type="text" value="field2"> <input type="text" value="field3"><br>
<input type="text" value="field1"> <input type="text" value="field2"> <input type="text" value="field3"><br>
<input type="text" value="field1"> <input type="text" value="field2"> <input type="text" value="field3"><br>
<input type="text" value="field1"> <input type="text" value="field2"> <input type="text" value="field3"><br>
</form>

then when you submit, you can retrieve all the values for field1 as

String[] field1Values = request.getParameterValues("field1");

CJ
0
 

Author Comment

by:changcy77
ID: 8059818

Thanks. But did you mistaken "value" with "name"?  
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 200 total points
ID: 8059824
try out this working code and you'll have a better idea where to go, it works the why you want only that it once add one row:
<%
/**************************************************/
String action=request.getParameter("action");
String[] itemname = request.getParameterValues("itemname");
String[] qty =  request.getParameterValues("qty");
String[] unit = request.getParameterValues("unit");
String[] unitprice = request.getParameterValues("unitprice");
String[] offerprice = request.getParameterValues("offerprice");
String[] chkbox = request.getParameterValues("chkbox");

boolean doDelete = "Delete Selected Item".equals( action );
boolean[] isChecked = null;

if( itemname != null ) isChecked = new boolean[ itemname.length ];
if( doDelete && chkbox != null ) {
  for( int jj = 0; jj<chkbox.length; jj ++ ) {
       int index = Integer.parseInt( chkbox[jj] );
       // out.println( "got " + index );
       isChecked[index] = true;
  }
}

if (action==null) action="";

/*****************************************************/
%>

<form method="post" name=formnewquote>

<% //if (action.equals("Submit")){ %>
<%  //@include file = "process_nq1.jsp"%>
<% //} %>

<TABLE cellPadding=3 width=600 border=0>
<TBODY>
<TR>
  <TD align=middle bgColor=#cecece colSpan=2 height=25><FONT face="Verdana, Arial, Helvetica, sans-serif">
      <B><FONT size=2>Item List</FONT></B></FONT></TD></TR></TBODY></TABLE>

<!-- Start List of Item(s) -->
<table id="tbl" width="600" border="0" cellspacing="1" bgcolor="#CCCCFF">
  <tr bgcolor="#8080C0">
    <td width="4%"><div align="center"></div></td>
    <td width="24%"><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Item Name </font></div></td>
    <td width="7%"><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Qty</font></div></td>
    <td width="21%"><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Unit</font></div></td>
    <td width="16%"><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Unit Price (RM) </font></div></td>
    <td width="17%"><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Offer Price (RM) </font></div></td>
  </tr>
  <%
int ii = 0;
if (itemname!=null) {
  for (int k=0;k<isChecked.length;k++) {
     if ( ! ( doDelete && isChecked[k] ) ) {
       %>
  <tr>
    <td> <div align="center">
        <input type="checkbox" name="chkbox" value="<%=ii%>">
      </div></td>
    <td><div align="center">
        <input name="itemname" type=text value="<%=itemname[k]%>" size="17">
      </div></td>
    <td><div align="center">
        <input name="qty" type=text value="<%=qty[k]%>" size="5">
      </div></td>
    <td><div align="center">
      <select name="unit" >
          <option value="kg" <% if ("unit[k]".equals("kg")) out.println("selected");%>>Kg</option>
          <option value="ctn" <% if ("unit[k]".equals("kg")) out.println("selected");%>>Cartons</option>
          <option value="pkt" <% if ("unit[k]".equals("kg")) out.println("selected");%>>Packet</option>
          <option value="tons" <% if ("unit[k]".equals("kg")) out.println("selected");%>>Tons</option>
        </select>
      </div></td>
    <td><div align="center">
        <input name="unitprice" type=text value="<%=unitprice[k]%>" size="8">
      </div></td>
    <td><div align="center">
        <input name="offerprice" type=text value="<%=offerprice[k]%>" size="8">
      </div></td>
  </tr>
  <%
         ii ++;
     }
  } //for
} //if

if( "Add".equals( action )  || itemname == null ) {
%>
  <tr>
    <td><div align="center">
        <input type="checkbox" name="chkbox" value="<%=ii%>">
      </div></td>
    <td><div align="center">
        <input name="itemname" type="text" size="17">
      </div></td>
    <td><div align="center">
        <input name="qty" type="text" size="5">
      </div></td>
    <td><div align="center">
      <select name="unit">
          <option value="kg">Kg</option>
          <option value="ctn">Cartons</option>
          <option value="pkt">Packet</option>
          <option value="tons">Tons</option>
        </select>
      </div></td>
    <td><div align="center">
        <input name="unitprice" type="text" size="8">
      </div></td>
    <td><div align="center">
        <input name="offerprice" type="text" size="8">
      </div></td>
  </tr>
  <% } %>
  <td colspan="6"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">
      <a href="javascript:checkall('formnewquote','chkbox',true)">All</a>/<a
      href="javascript:checkall('formnewquote','chkbox',false)">None</a></font>
  <input type=submit value="Delete Selected Item" name="action"  style="font-family: Verdana,
      Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;background-color:#CCCCFF";"></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td colspan="6"><input type=submit value="Add" name="action" style="font-family: Verdana,
      Arial, Helvetica, sans-serif;font-size: 10px;color: #000000;background-color:#CCCCFF";"></td>
  </tr>
</table>
</form>
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8059839
yes value should be name
0
 

Author Comment

by:changcy77
ID: 8060148
there are 5 fields. for example:
the table looks like this:

field1 field2 field3 field4 field5
------ ------ ------ ------ -----
data1   data2  data3  data4  data5
data11  data22 data33 data44  data55
........

Are you saying that all the text input of the same field, should be named that same? And I will get a long string which concantenate all data from the same field? like data1,data11, data111....

Thanks.







0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8060300
have you tried my sample code, just copy them to a jsp page and you can see result immediately.

it can let user add/delete rows and process them.
0
 

Author Comment

by:changcy77
ID: 8064332
Thanks. Kennethxu and CJ.
The String[] works, but not not StringTokenizer for my case. I will accept Kennethux's answer which is more clear to me. I guess sample codes always help, next time CJ!
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8064952
no problem, as long as you found a solution- I am here to help.  Pts aren't important to me.  I have enough of them already.  :-)

CJ
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8065057
thanks changcy!

CJ, how about I post 25 points for you? thanks.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8065330
thanx for the offer but no need.

As long as a solution was found.  Thats all that matters.

CJ
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

800 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