FishingIsLife
FishingIsLife

Reputation: 2372

Jsp send value of dynamic checkboxes to servlett

I'm totally new in the world of html/jsp/servlett programming. Actually I'm working on a project where I create dynamic checkboxes. Each of them should have a different value.

<c:forEach items="${sessionScope.camlist}" var="cam">
                  <form id="updatePermission" method="post" action="<%=request.getContextPath() %>/PermissionHandling?operation=update_permissions&id=${cam.kameraid}" >
                    <table>
                        <td>${cam.kameraid}</td>
                                <script>
                                    console.log("${cam.kameraid}");

                                    </script>
                        <td>'${cam.standort}'</td>
                        <td>'${cam.url}'</td>
                        <td>${cam.aufnahmeinterval}</td>
                        <td>
                        <c:set var="checked" value="false"/>
                        <c:forEach items="${sessionScope.permissionList}" var="perm">
                            <c:if test="${perm.kameraid eq cam.kameraid}">
                            <input type="checkbox" name="selection" checked="checked" value="${cam.kameraid}" onClick="callServlett()">
                            <c:set var="checked" value="true"/>
                            <c:set var="continueExecuting" scope="request" value="false"/>
                             </c:if>
                        </c:forEach> 
                        <c:if test="${checked eq false}">
                            <input type="checkbox" name="selection" value="${cam.kameraid}" onClick="callServlett()">
                        </c:if>
                        </td>                
                     </table>
                    </form>
                  </c:forEach>

            </div>
         </section>
        <script>

        function callServlett()
        {
            document.getElementById("updatePermission").submit();
        }
        </script>

So with doing that, I get a table where the checkboxes of each row are initializied depending wether a user has permissions or not. When a checkbox gets modified by a click I want to submit the calue of the cameraid to my servlett which handels the rest of the work.

Servlett:
String[] selections = request.getParameterValues("selection");

Selections is always "3" except when I klick the first checkbox, then it's null.

Can anyone tell me why this code doesn't work as I expect?

Upvotes: 0

Views: 61

Answers (1)

Srinu Chinka
Srinu Chinka

Reputation: 1491

You need to put <form> tag outside of the forEach loop. If you put inside each loop will create new form tag and it may not work as you wanted.

<form id="updatePermission" method="post" action="<%=request.getContextPath() %>/PermissionHandling?operation=update_permissions&id=${cam.kameraid}" >
       <c:forEach items="${sessionScope.camlist}" var="cam">

       <!-- html code -->
       </c:forEach>
</form>

Instead of calling servlet for every click on checkbox keep a button and call servlet when you click on the button. This will reduce the number of iterations from browser to server.

Upvotes: 1

Related Questions