0

Hey:) I'm using JSTL and i created a table that has five colums(iprodID,name,desc,price,image) with data from a database.What i want to do is to add another column with checkboxes for each row and allow the user to select as many rows of data he/she wants from the table. Below the table is a from, when he clicks the submit button, his input on the form and the data from the checkboxes should be placed in another database.how do i do that?thanks:)

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by javaAddict
0

Usually when you have a check box and you submitted, then if it was not checked the request.getParameter("paramName") will return null, Otherwise it will return a not null value.
Assuming that you have those check boxes in a for loop with index 'i', you can have the check box be like this:

<input type="checkbox" name="checked_<%=i%>" />

If you pass the total number of check boxes at the request, you can have a for loop like this:

for (int i=0;i<N;i++) {
  String check = request.getParameter("checked_"+i);
  if (check!=null) {
    // check box was check, save value
  }
}

In order the get the other values you can do the same:
example:

<td>
<input type="text" name="name_<%=i%>" value="<%=theObjects[i].getName()%>" />
</td>
<td>
<input type="text" name="desc_<%=i%>" value="<%=theObjects[i].getDesc()%>" />
</td>

And take those values in the same way you take the values of the check boxes.
In total:

Page1.jsp:

<form action="Page2.jsp">
  <input type="hidden" name="length" value="<%=theObjects.length%>" />
.....
   <%
      for (int i=0;i<theObjects.length;i++) {
   %>
       <tr>
<td>
<input type="text" name="name_<%=i%>" value="<%=theObjects[i].getName()%>" />
</td>
<td>
<input type="text" name="desc_<%=i%>" value="<%=theObjects[i].getDesc()%>" />
</td>
<td>
<input type="checkbox" name="checked_<%=i%>" />
</td>
       </tr>
   <%
      }
   %>
.....
</form>

Page2.jsp

int N = Integer.parseInt(request.getParamater("length"));

for (int i=0;i<N;i++) {
  String check = request.getParameter("checked_"+i);
  if (check!=null) {
    // check box was check, save value
    String name = request.getParameter("name_"+i);
    String desc = request.getParameter("desc_"+i);
  }
}

Each row will have the same 'i'. So you can use that to take the value of each element in the same row, whose the check box was checked. Assuming that theObjects is the array of objects where you have stored the data.

Also I am not very familiar with JSTL, so you need to make the necessary changes. But the idea is the same.

Edited by javaAddict: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.