views:

104

answers:

3

Hi I have two check boxes with table names when any check box is checked i wants to save that table name in to a java string which i can use in the query to get data from that table or update that table. I have used onClick functions and also got the check box value but not getting how to access it in the rest of the code so that i can use that value in the DB query.

A: 

Set a hidden parameter onclick of checkbox and pass that hidden parameter,fetch that param at server side from request.

org.life.java
+1  A: 

I assume you have HTML code like the following:

<input type="checkbox" name="use_table1" />
<input type="checkbox" name="use_table2" />

On the server-side Java code, you can then query:

String tableName = null;
if (request.getParameter("use_table1") != null)
  tableName = "tbl_1";
if (request.getParameter("use_table2") != null)
  tableName = "tbl_2";

Note that the "outside" names differ from the real table names. No one out there on the web should need to know your real table names. And, most important, no one should be allowed to read an arbitrary table from your database. That's why I used that if-then-else code to select the table name.

Roland Illig
A: 

Just give the table name as value. The browser will send only the name-value pairs of the checked ones as request parameters.

<input type="checkbox" name="tablename" value="table1">
<input type="checkbox" name="tablename" value="table2">
<input type="checkbox" name="tablename" value="table3">
<input type="checkbox" name="tablename" value="table4">

In the Servlet, you can grab them by HttpServletRequest#getParameterValues().

String[] checked = request.getParameterValues("tablename");

Simple as that :) No need for unnecessary JavaScript hacks/workarounds which ain't going to work in JS-disabled clients. This is one of the many unknown features of HTML.

BalusC