James McTyre
James McTyre

Reputation: 103

Get all checked checkbox values to servlet from dynamically generated JSP table?

I'm displaying the results of my query from Table A in my results.jsp as goes:

<form action="InsertToTableB" method="post">
       <table>
            <tr>
                <th>Select</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>

            <c:forEach var="user" items="${resultData}">
            <tr>
                <td><input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/></td>
                <td><c:out value="${user.fName }"/></td>
                <td><c:out value="${user.lName }"/></td>
                <td><c:out value="${user.email }"/></td>    
            </tr>
            </c:forEach>    
        </table>
   <button type="submit" value="Submit">Insert to Table B</button>
   <button type="submit" value="Submit">Delete from Table A</button>
</form>

And this form is being passed into my InsertToTableB servlet.

How do I get ONLY the selected values from the generated table <input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/>and how do I know which button (Insert to Table B or Delete from Table A) has been pressed?

I've only worked with <input type="text"> and getting the value into the servlet using .getParameter('name') and those are Strings.

Is it the same for checkbox input type?

Can I have 2 or more submit buttons in a form each with a different function?

A picture of my table with sample data:

enter image description here

Upvotes: 0

Views: 1805

Answers (1)

flyingfox
flyingfox

Reputation: 13506

You need change name='selected' to checked='checked'

so change

<input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/>

to

<input type="checkbox" checked="checked" value="<c:out value='${user.userID }' />"/>

To get all the checked checkbox,you can use jQuery like below:

var checkedEles = $("input:checkbox:checked");

Upvotes: 1

Related Questions