2

I'd like to know if there's any way to send data to the server for the selected rows using the checkboxes I've put on those rows? I mean , how can I send only the data (in this case mileage ) of those selected rows (selected with the checkboxes) to the server ? see the image

Here's the html code I use:

<table>  
  <thead>  
    <tr class="tableheader">  
      <td width="10%"></td>  
      <td width="30%">Vehicle</td>  
      <td width="40%">Driver</td>  
      <td width="10%">Mileage</td>  
     </tr>  
  </thead>  
  <tbody>  
    <c:forEach items="${list}" var="item">  
         <tr>  
             <td align="center">  
                <input type="checkbox" name="selectedItems"   
                    value="c:out value="${item.numberPlate}"/>"/>  
             </td>  
             <td align="left"><c:out value="${item.numberPlate}"/></td>  
             <td align="left"><c:out value="${item.driver.fullName}" /></td>  
             <td align="left"><input type="text" name="mileage" value="" /></td>  
          </tr>  
     </c:forEach>                         
   </tbody>  
</table>  

I really hope you can give some guidance on this.

Thanks in beforehand.

eddy
  • 4,373
  • 16
  • 60
  • 94

3 Answers3

8

Change the mileage input as follows:

<c:forEach items="${list}" var="item">  
    <tr>  
        <td align="center">  
            <input type="checkbox" name="selectedItems"   
                value="<c:out value="${item.numberPlate}"/>"/>  
        </td>
        <td align="left"><c:out value="${item.numberPlate}"/></td>  
        <td align="left"><c:out value="${item.driver.fullName}" /></td>  
        <td align="left"><input type="text" name="mileage_<c:out value="${item.numberPlate}"/>" value="" /></td>  
    </tr>  
</c:forEach>

Gather it as follows:

String[] selectedItems = request.getParameterValues("selectedItems");
for (String selectedItem : selectedItems) {
    String mileage = request.getParameter("mileage_" + selectedItem);
    // ...
}

No need for nasty JS workarounds.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a million and merry xmas to everyone – eddy Dec 24 '10 at 18:13
  • @BalusC-Could you please, give me a hand over here : http://stackoverflow.com/questions/4528399/how-to-check-if-a-checkbox-in-a-checkbox-array-is-checked-with-jquery – eddy Dec 24 '10 at 23:07
0

According to HTML's FORM spec, items with same name will be grouped to single value separated by comma. So just bind your bean to form result and split value of 'selectedItems' by comma.

Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • Thanks, but what about mileage? how can I make sure that the values (mileage) I sent to the server are only the corresponding to the selected rows (selected with the checkboxes)? – eddy Dec 24 '10 at 09:19
  • @eddy - there are lot of options, to implement it. You still can manage mileage as comma separated value passed to FORM. Another way is create unique naming like milage_ and after iterating FORM's collection extract all of them. – Dewfy Dec 24 '10 at 09:27
0

First off, I'd suggest to make sure that your server-side script ignores the "non-checked" data, because you never can be sure that the JavaScript will actually work or isn't manipulated by the user.

However currently you have no way too associate each check box with its mileage input, so you should consider giving each an individual name such as {numberplate}-mileage.

That would also simplify the JavaScript. (I'm using jQuery here, becuase it quicker):

jQuery("input[name=selectedItems]").click(
   var chkbox = $(this);
   $("input[name=" + checkbox.val() + "-mileage]").attr("disabled", checkbox[0].checked ? "" : "disabled");
);

(Disabled input elements don't submit their value).

RoToRa
  • 37,635
  • 12
  • 69
  • 105