Solved

get the number of checked boxes using jsp

Posted on 2003-10-30
7
541 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 51

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 51

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery - parsing the results returned from webmethod 1 118
EJB MDB example 4 87
constructor overloading 2 86
iterator example remove 8 75
This article describes how to reset your Windows 10 password when you've forgotten it.
Riverbed Technology's webinar discusses networking for the cloud era with simplified SD-WAN cloud connectivity.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 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