Passing checkbox values from one JSP page to another

Hi,

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

================================================== ======

<% 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());

}

%>

<form name="form1" method="post" action="B.jsp">

<label>

<input type="submit" name="Select" >

</label>

</form>

</BODY>

================================================== ======

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.

[3076 byte] By [delrama] at [2007-11-27 5:50:28]
# 1

Easiest way is to do this when the form is submitted.

Each checkbox that is checked will send through its name/value pair.

String[] selectedCheckboxes = request.getParameterValues("Select");

That should give you an array of Strings, each entry in the array representing the value of one of the checked checkboxes.

evnafetsa at 2007-7-12 15:37:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi evnafets,

Did you mean something like this in the 2nd (B.jsp)?

==========================================

String selectedCheckboxes= request.getParameterValues("Select");

for(int n=0;n<selectedCheckboxes.length;n++)

out.println(">
" + selectedCheckboxes[n]);

===========================================

I think I need to check what I'm passing because the array has a length of 1 and contains just the name of the Submit button in A.jsp

Can you pls. point me to where I'm going wrong? Thanks!

delrama at 2007-7-12 15:37:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
String selectedCheckboxes= request.getParameterValues("Select");the above code should be like thisString[] selectedCheckboxes= request.getParameterValues("Select");
senthil_yogaa at 2007-7-12 15:37:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi senthil_yoga

Thank you.

Can you see what I'm passing wrong ? I seem to be passing the name of the Submit button from A.jsp to B.jsp

I'm able to get ALL the values from ALL the checkboxes. I'm not sure how I pass only the ones that are checked.

I need to isolate and pick ONLY those values fo the SELECTED checkboxes. Any inputs will really be appreciated.

Thank you!

D

delrama at 2007-7-12 15:37:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Two things

1 - HTML will only submit controls that are nested within the form. So your checkboxes have to be generated between the <form> and the </form> to be picked up

2 - You should have a different name for the checkbox than you do for the submit button so you can distinguish between them :-)

So

1 - Shift the <form> tag up to the top of the page.

2 - Change <input type="submit" name="Select" > to have a different name eg <input type="submit" name="submitButton" >

evnafetsa at 2007-7-12 15:37:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thank You!!!!That solved it. All I needed to do was move the form tag up.Thanks a lot !!!:-)D
delrama at 2007-7-12 15:37:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...