By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Passing checkbox values from one JSP page to another

P: 10
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

Expand|Select|Wrap|Line Numbers
  1. ========================================================
  2.  <% String url = "jdbc:mysql://...";
  3. String user= "";
  4. String pass= "";
  5. String temp = "_";
  6. try{
  7.       Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  8.       Connection conn = DriverManager.getConnection(url, user, pass);
  9.       Statement stmt = conn.createStatement();
  10.       ResultSet rs = stmt.executeQuery("SELECT * FROM <tablename>");
  11.  
  12.       while(rs.next()){ %>
  13.                         <table><tr>
  14.                         <td><b><%= rs.getString("ID") %></b></td>
  15.                         <td><%= rs.getString("DESCRIPTION") %></td>
  16.                         <td> <input name="Select" type="checkbox" value="<%=rs.getString("ID")%> checked"></td>
  17.                         </tr></table>
  18.  
  19.                          <% String selection = rs.getString("ID");
  20.                         temp = temp + selection + "_"; 
  21.                          }                         
  22.                         rs.close();
  23.                         conn.close();
  24.                         }catch(Exception e)
  25.                                        {out.println(e.toString());   
  26.                                        }
  27. %>
  28. </p>
  29. <p>&nbsp; </p>
  30.     <form name="form1" method="post" action="B.jsp">
  31.       <label>
  32.       <input type="submit" name="Select" >
  33.       </label>
  34.     </form>
  35.     </BODY>
  36. ========================================================
  37.  
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.
May 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: 10
Figured out.
Thanks..
May 30 '07 #2

P: 1
how did u do that I have the same problem. Can u please explain me. thanks in advance.
Jun 2 '10 #3

jkmyoung
Expert 100+
P: 2,057
Are you using a form, and creating an input like so
<input type="checkbox" name="field1"...>
??
Jun 2 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.