• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing checkbox data from JSP to SELECT statement

 
Dave Jordan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help please!
I have a form where the user checks any or all of 6 boxes, I then need to take the user's choices and use them in a SELECT statement in a query.
the JSP looks like:
<table border="2" style="background-color:#FFFF99;">
<tr>
<TD WIDTH=85% ALIGN="LEFT">
<B>Check Departments To Include:</B><br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="csr">Customer Service<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="clm">Claims<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="bne">Billing & Enrollment<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="adj">Adjustments<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="apl">Appeals<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="sub">Subrogation<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="acg">Accounting<br>
</td></tr>
</table>
This is passed to a controller, which then calls a method which contains the following select statement:
sql.append("SELECT bla, bla, bla FROM bla, bla, bla");
sql.append("WHERE ( dept_code IN (?);

The selected department codes would replace the question mark. It seems like this should be easy, right? All help is Greatly appreciated!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your question regard how to get the form submission result, or how to format the SELECT statement?
bear
[ December 05, 2003: Message edited by: Bear Bibeault ]
 
Tim Baker
Ranch Hand
Posts: 541
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
each checkbox needs it own name if your going to allow more than one to be checked. then you use getParameter and if it returns something then they checked it and if it returns null then they didnt. then use a prepared statement to set the boolean value in your SQL
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd change the following line:
each checkbox needs it own name if your going to allow more than one to be checked.
to:
if you want to make retrieval easy, give each checkbox its own name

but then again, based on my last paragraph, this should be:
sometimes retrieval is easier when giving each checkbox its own name - other times it's not

It's not a requirement to give mutually selectable checkboxes different names (unlike radio buttons).

form.html

foo.jsp


You'd just have to make each checkbox value be the primary key of the department, and append it into the "in clause" string you're constructing

In fact, that might be easier, than requesting each and every department, and having to decide whether to include it or not (which you'd need to do if each checkbox had a different name). Also, if you add a department, you'd need to change the processing jsp. With checkbox of the same name, only the initial list of departments need change.
[ December 05, 2003: Message edited by: Mike Curwen ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a follow-up.

If you need to have the full names of what the checkboxes mean, in the second page... that second page will query the database to retrieve the full name. If you weren't loading them from a database, then just use a bunch of if statements.

(This was prompted by a private message from the original poster: who has private messages turned off).
 
Dave Jordan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the full names are not coming from a database. Looks like I'll have to go with the multiple "if" statements. Thanks for your help.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic