Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

get the number of checked boxes using jsp

Posted on 2003-10-30
7
Medium Priority
?
546 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
Industry Leaders: 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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