Nayeem Hyder Riddhi
Nayeem Hyder Riddhi

Reputation: 621

How to get checkboxes values for JSP page?

I can get a single value for an item in jsp page by id. But I can't able to get checkboxes value. How can I get checkboxes value by id or anything else. when i will click in order page it will collect all products name and product price

Screenshot

enter image description here Code is:

<table class="table table-hover table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Choose Product</th>
                    <th>Product Name</th>
                    <th>Product Price</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <%
                String Host = "jdbc:mysql://localhost:3306/shopbilling";
                Connection connection = null;
                Statement statement = null;
                ResultSet rs = null;
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(Host, "root", "");
                statement = connection.createStatement();
                String query = request.getParameter("q");
                String data;
                if(query != null)
                {
                data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
                }
                else 
                {
                data = "select * from products_tbl";
                }
                rs = statement.executeQuery(data);
                while (rs.next()) {
                %>
                <tr>
                    <td><%=rs.getString("id")%></td>
                    <td> <input type="checkbox" /> </td>
                    <td><%=rs.getString("product_name")%></td>
                    <td><%=rs.getString("product_price")%></td>
                    <td class="text-center" width="250">
                       <a href='edit.jsp?u=<%=rs.getString("id")%>' class="btn btn-warning">Edit</a>
                        <a href='delete.jsp?d=<%=rs.getString("id")%>' class="btn btn-danger">Delete</a>
                    </td>
                </tr>

                <%
                }
                %>
            </tbody>

        </table>
       <a href='order.jsp' class="btn btn-success">Order</a>

Upvotes: 1

Views: 84

Answers (1)

Rishabh Ryber
Rishabh Ryber

Reputation: 466

Here you can use value tag of <input type="checkbox"> to put the data to user.Then you need to give a attribute 'name' to all of these check-boxes, which you can keep same and then you can grab all the selected values as an array.

Here is a changes you need to make in this code::

<table class="table table-hover table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Choose Product</th>
                    <th>Product Name</th>
                    <th>Product Price</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <%
                String Host = "jdbc:mysql://localhost:3306/shopbilling";
                Connection connection = null;
                Statement statement = null;
                ResultSet rs = null;
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(Host, "root", "");
                statement = connection.createStatement();
                String query = request.getParameter("q");
                String data;
                if(query != null)
                {
                data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
                }
                else 
                {
                data = "select * from products_tbl";
                }
                rs = statement.executeQuery(data);
                while (rs.next()) {
                %>
                <tr>
                    <td><%=rs.getString("id")%></td>
                    <td> <input type="checkbox"  name="products" value ="<%=rs.getString("id")%>" /> </td>
                    <td><%=rs.getString("product_name")%></td>
                    <td><%=rs.getString("product_price")%></td>
                    <td class="text-center" width="250">
                       <a href='edit.jsp?u=<%=rs.getString("id")%>' class="btn btn-warning">Edit</a>
                        <a href='delete.jsp?d=<%=rs.getString("id")%>' class="btn btn-danger">Delete</a>
                    </td>
                </tr>

                <%
                }
                %>
            </tbody>

        </table>
       <a href='order.jsp' class="btn btn-success">Order</a>

And while receiving the result you need to use something like this::

<%

    String products[] = request.getParameterValues("products"); 
    if (products!= null && products.length != 0) {
        out.println("You have selected: ");
        for (int i = 0; i < products.length; i++) {
             out.println(products[i]); 
        }
    }
%>

Hope this helped you!! Happy Coding:)

Upvotes: 1

Related Questions