Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

how to submit multiple rows of data at one click

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
changcy77
Asked:
changcy77
  • 6
  • 6
  • 3
1 Solution
 
cheekycjCommented:
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
 
changcy77Author Commented:
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
 
cheekycjCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
changcy77Author Commented:
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
 
changcy77Author Commented:
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
 
cheekycjCommented:
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
 
changcy77Author Commented:

Thanks. But did you mistaken "value" with "name"?  
0
 
kennethxuCommented:
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
 
cheekycjCommented:
yes value should be name
0
 
changcy77Author Commented:
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
 
kennethxuCommented:
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
 
changcy77Author Commented:
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
 
cheekycjCommented:
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
 
kennethxuCommented:
thanks changcy!

CJ, how about I post 25 points for you? thanks.
0
 
cheekycjCommented:
thanx for the offer but no need.

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

CJ
0

Featured Post

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!

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now