?
Solved

get the number of checked boxes using jsp

Posted on 2003-10-30
7
Medium Priority
?
545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 53

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 60 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 53

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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