Solved

get the number of checked boxes using jsp

Posted on 2003-10-30
7
538 Views
Last Modified: 2010-04-01
i have made a form in jsp with checkboxes.how will i count the number of boxes that are checked and pass it to another page?
thanks.hope you can help me on this one.
0
Comment
Question by:kurisuchine22
  • 4
  • 2
7 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 9655167
You can try like:

String[] myarray = request.getParameterValues("chk");

if (myarray.length > 0) {
      for (int i=0;i<myarray.length;i++) {
            out.println(subs[i]);
      }
}
0
 
LVL 3

Accepted Solution

by:
applekanna earned 30 total points
ID: 9655431
in your page1.jsp

<form method = post action = page2.jsp>
<input type = checkbox name = "ckbox" value = "one">
<input type = checkbox name = "ckbox" value = "two">
<input type = checkbox name = "ckbox" value = "three">
<input type = checkbox name = "ckbox" value = "four">
</form>

in your servelt or in the next JSP
"page2.jsp"
<%
String arr[] = request.getParameterValues("ckbox");

int count = arr.length(); // here you have ">count the number of boxes that are checked"

%>


0
 
LVL 3

Expert Comment

by:applekanna
ID: 9655452
ryancys

Kindly correct me if I am worng.

the method "request.getParameterValues("ckbox");" would return only those values that are checked or radiobox or chekcbox right?

Thx.

kurisuchine22 ,
you can do this in JavaScript also.


<form method = post action = page2.jsp onsubmit  = "javascript:count()">
<input type = checkbox name = "ckbox" value = "one">
<input type = checkbox name = "ckbox" value = "two">
<input type = checkbox name = "ckbox" value = "three">
<input type = checkbox name = "ckbox" value = "four">

<input type = hidden name = "count" value = "0">

</form>

in between the <head > and </head> of html add the sript

<script language = "javascript">
function count()
{
var count = 0;
for(int i = 0; i< document.ckbox.length() ;i++)
 if(document.ckbox[i].value.checked == true)
  count++;
 document.count.value = count;
return count
}
</script>

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:applekanna
ID: 9655454
code snippet also available from

http://javascript.internet.com/forms/checkbox-counter.html

Hope this helps
Cheers!
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 9655747
>>the method "request.getParameterValues("ckbox");" would return only those values that are checked or radiobox or chekcbox right?

That's true, not only in JSP, in other scripting language like ASP, only selected items will be included as parameter. Use request.getParameterValues(parametername) if more that 1 instance of parametername is available on the submit form, while use common request.getParameter(parametername) if only 1 instance of parametername is found in that particular form.

Hope this is clear.

cheers
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9655757
Crystal clear :) Thx
0
 
LVL 1

Author Comment

by:kurisuchine22
ID: 9714238
sorry but i can't give the points to you all,i have tried it all and none of this worked.though applekanna is partly right,it still didn't work.

i have tried this code and it worked:
String[] arr = request.getParameterValues("myarray");
int arrnum = arr.length;

because .length() is a class. so i used .length instead.
but i appreciate all of you for taking time to answer my question.hope you can help me on my other questions.no hard feelings.

i know we are all here to learn.:-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now