Passing checkbox values from one JSP page to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • delram
    New Member
    • May 2007
    • 10

    Passing checkbox values from one JSP page to another

    I'm trying to get user selections (using checkboxes) on one JSP page(A.jsp) and pass them on to another page.


    So I have one page with a list of dynamically populated items like this

    1 [checkbox]
    2 [checkbox]
    ...
    ...
    n [checkbox]

    Submit button


    So the second page(B.jsp) needs to say " You selected 1,2,3" (apart from stuffing them into a database)

    Here's a snippet of code from A.jsp

    Code:
    ========================================================
     [I]<% String url = "jdbc:mysql://...";
    String user= "";
    String pass= "";
    String temp = "_";
    try{
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
          Connection conn = DriverManager.getConnection(url, user, pass);
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery("SELECT * FROM <tablename>");
    	 
          while(rs.next()){ %>
                            <table><tr>
                            <td><b><%= rs.getString("ID") %></b></td>
                            <td><%= rs.getString("DESCRIPTION") %></td>
                            <td> <input name="Select" type="checkbox" value="<%=rs.getString("ID")%> checked"></td>
                            </tr></table>
                             
                             <% String selection = rs.getString("ID");
    						temp = temp + selection + "_"; 
    						 }						 
                            rs.close();
                            conn.close();
                            }catch(Exception e)
                                           {out.println(e.toString());   
                                           }
    %>
    </p>
    <p>&nbsp; </p>
        <form name="form1" method="post" action="B.jsp">
          <label>
          <input type="submit" name="Select" >
          </label>
        </form>
        </BODY>[/I]
    ========================================================
    The table in question has 2 columns - ID and DESCRIPTION. I also need to capture the IDs from all the user selections and pass them on to page 2 as a string

    If the user selects items 1, 2, 3, I need to pass _1_2_3

    I'm not familiar with checkboxes and unable to capture only those that the user selects. In the example above, I'm trying to capture only those IDs from the checkboxes the user selects under temp. Right now, every single ID is appended to temp and it looks like _1_2_3_...._n.


    Can you pls. take a look at the code?
    Thank you for your time!

    D.
    Last edited by Niheel; Jun 2 '10, 03:35 AM. Reason: code tags
  • delram
    New Member
    • May 2007
    • 10

    #2
    Figured out.
    Thanks..

    Comment

    • kriti001
      New Member
      • Jun 2010
      • 1

      #3
      hi

      how did u do that I have the same problem. Can u please explain me. thanks in advance.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Are you using a form, and creating an input like so
        <input type="checkbox" name="field1".. .>
        ??

        Comment

        Working...