Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox 1

Status
Not open for further replies.

sensory21

Programmer
Jan 27, 2004
79
GB
Hi all,

I need help with checkboxes in my jsp code, what I need to do is to assign the value of the customer_id from my sql database to each checkboxes in my page so when a/ or more checkbox(es) are checked I can delete that customer?

The checkboxes are displayed from a loop.

Can anyone help?

Cheers
Vero
 
What exactly are you having problems with ?
 
For the moment I've 3 checkboxes displayed and when I check 1 box it says checkbox = on, on all the boxes; i am using


String ckCheckBox = request.getParameter("ckCheckBox");
out.println("ckCheckBox= " + ckCheckBox);

<input type=\&quot;checkbox\&quot; name=\&quot;ckCheckBox\&quot;&quot;);
 
Take a look at the examples below - these idenitfy checkboxes correctly ...

Code:
<!-- test.html -->
<html>
<body>

	<form action=&quot;test1.jsp&quot; method=&quot;post&quot;>
		<input type=&quot;checkbox&quot; name=&quot;cb1&quot;>
		<input type=&quot;checkbox&quot; name=&quot;cb2&quot;>
		<input type=&quot;submit&quot;>
	</form>
</body>
</html>

Code:
<!-- test1.jsp-->
<html>
<body>
<%

out.println(&quot;cb1 - &quot; +request.getParameter(&quot;cb1&quot;) +&quot;, cb2 -&quot; +request.getParameter(&quot;cb2&quot;));

%>

</body>
</html>
 
Thanks Sedj,

I have already done that and now I am getting the result of the customer_id so if I check the first box it will print checkbox = 1 etc
but if I click on 2 boxes it will print the first id it founds!

<input type=\&quot;checkbox\&quot; name=\&quot;ckCheckBox\&quot; value=\&quot;&quot; + customerId + &quot;\&quot;>

that is in my while loop
 
There must be something wrong with how you are setting up your checkboxes, becasue the code below works fine :

Code:
<html>
<body>

	<form action=&quot;test1.jsp&quot; method=&quot;post&quot;>
		<input type=&quot;checkbox&quot; name=&quot;cb1&quot; value=&quot;cb1val&quot;>
		<input type=&quot;checkbox&quot; name=&quot;cb2&quot; value=&quot;cb2val&quot;>
		<input type=&quot;submit&quot;>
	</form>
</body>
</html>

Code:
<!-- test1.jsp -->
<html>
<body>
<%
out.println(&quot;cb1 - &quot; +request.getParameter(&quot;cb1&quot;) +&quot;, cb2 -&quot; +request.getParameter(&quot;cb2&quot;));

%>

</body>
</html>
 
Hi,

The Check Box should be a Group then you can get only those values of check box's that where checked. The Values of the CheckBox should be the customer_id that is comming from the SQL Query. Below I have set them to dummy values 1,2,3.

Ex:

<< test.jsp >>

<form method=&quot;POST&quot; action=&quot;test.jsp&quot;>
<p><input type=&quot;checkbox&quot; name=&quot;customer_id&quot; value=&quot;1&quot;></p>
<p><input type=&quot;checkbox&quot; name=&quot;customer_id&quot; value=&quot;2&quot;></p>
<p><input type=&quot;checkbox&quot; name=&quot;customer_id&quot; value=&quot;3&quot;></p>
<p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;></p>
</form>
<%
if(null != request.getParameterValues(&quot;customer_id&quot;))
{
String [] checkBox = request.getParameterValues(&quot;customer_id&quot;);
for(int i=0; i< checkBox.length ; i++)
{
// SQL Query to Delete the records where customer_id = checkBox
out.println(checkBox);
// it will print only those records which are checked
}
}
%>

Cheers,
Venu
 
Thank you very much Venu,

exactly what I was looking for, I thought I needed another loop with an array.

Have a great day!
Vero
 
String sql = &quot;delete FROM customer where customer_id = checkBox&quot;;

Can you see my syntax error?

Vero
 
Hi Vero,

It should be in for loop..
String sql = &quot;delete FROM customer where customer_id =&quot; + checkBox;

Cheers
Venu
 
If the customer_id is not a NUMBER field, you'll need

String sql = &quot;delete FROM customer where customer_id ='&quot; + checkBox +&quot;'&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top