Solved

get the number of checked boxes using jsp

Posted on 2003-10-30
7
540 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 50

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 50

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nested if has else if 13 94
main method of string class 3 110
I get error: useBean: Duplicate bean name: {0} 1 136
going to wrong jsp page 2 21
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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