Solved

when i select multiple check box's for multiple empId's  and enter submit how to capture the empNO's to the corresponding empId;

Posted on 2006-11-03
17
2,029 Views
Last Modified: 2012-05-05

execute the below sample jsp;

this is a sample jsp ;when i select multiple check box's for multiple empId's  and enter submit how to capture the empNO's to the corresponding empId;

<%for(int i=0;i<10;i++)
{%>
<tr class='formheader' align=center>
<td>
<input type="checkbox" id="empId<%=i%>" name="empId" value="<%=i%>"><%=i%>
<input type="hidden" id="empNO<%=i%>" name="empNO" value="AAA<%=i%>">
</td>
</tr>
<%}%>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>

 but i want only checked values here
<%
out.println(request.getParameterValues("empId"));
out.println(request.getParameterValues("empNO"));

%>
0
Comment
Question by:chaitu chaitu
  • 7
  • 7
  • 2
17 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 300 total points
ID: 17866886
<%
    for(int i=0;i<10;i++)
    {
%>
  <tr class='formheader' align=center>
    <td>
      <input type="checkbox" name="empId" value="<%=i%>"><%=i%>
      <input type="hidden" name="<%= "empNO" + i %>" value="AAA<%=i%>">
    </td>
  </tr>
<%
    }
%>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>

<%
    String[] ids = request.getParameterValues( "empId" ) ;
    for( String id : ids )
    {
        out.println( id + " : " + request.getParameter( "empNO" + id ) ) ;
    }
%>
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17872123
may i know what is meaning of this

for( String id : ids )
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17872188
below code not fetching me checked values;

<script>

function ex()
{
                        Url      =      'check.jsp';
                        document.forms[0].action = Url;
                        document.forms[0].submit();
}
</script>

<form action="second_page.jsp" method="post">

<%
    for(int i=0;i<10;i++)
    {
%>
  <tr class='formheader' align=center>
    <td>
      <input type="checkbox" name="empId" value="<%=i%>"><%=i%>
      <input type="hidden" name="empNO<%=i%>" value="AAA<%=i%>">
    </td>
  </tr>
<%
    }
%>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>

<%
    String[] ids = request.getParameterValues( "empId" ) ;
      if(ids != null && ids.length > 0)
      {
    for(int id=0;id<ids.length;id++ )
    {
        out.println( id + " : " + request.getParameter( "empNO" + id ) ) ;
    }
      }
%>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 17872875
>> for( String id : ids )

loops through the ids array, putting each value into id in turn

You need Java 1.5+
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 17873733
>>>
for(int id=0;id<ids.length;id++ )
    {
        out.println( id + " : " + request.getParameter( "empNO" + ids[id] ) ) ;
    }
     }
%>

should work.
I thought I answered the same question a couple of days ago for you...!!!
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17875372
thats for radio button here checkboxes;;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 17875861
So I get the small share of the points do I chaituu

Thanks...

!
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17875887
its actually

out.println( id + " : " + request.getParameter( "empNO" + ids[id] ) ) ;

not

out.println( id + " : " + request.getParameter( "empNO" + id ) ) ;
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 35

Expert Comment

by:TimYates
ID: 17875924
it depends if you use the Java 5 for loop like I had, or the old style for loop like you had

Yes, putting my code into your loop didn't work

But that's hardly suprising
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17875952
this sentence is important naa to display o/p ;

request.getParameter( "empNO" + ids[id] )
0
 
LVL 35

Expert Comment

by:TimYates
ID: 17875956
<%
    String[] ids = request.getParameterValues( "empId" ) ;
    for( String id : ids )
    {
        out.println( id + " : " + request.getParameter( "empNO" + id ) ) ;
    }
%>

Will work with Java 5

...as I said before
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17875995
rightnow i am working in java 1.5 only;;

for( String id : ids )  

equals to

for(int id=0;id<ids.length;i++) in old style;


i think  request.getParameter( "empNO" + id )  it is wrong;;


0
 
LVL 35

Expert Comment

by:TimYates
ID: 17875998
   for( String id : ids )
        out.println( request.getParameter( "empNO" + id ) ) ;

in 1.5 is EXACTLY the same as:

    for( int id = 0 ; id < ids.length ; id++ )
        out.println( request.getParameter( "empNO" + ids[ id ] ) ) ;

in 1.4
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17876001
ok i will ask the moderator to reopen the question
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 200 total points
ID: 17876723
As Mr. Tim is saying....

  for( String id : ids )
        out.println( request.getParameter( "empNO" + id ) ) ;

in 1.5 is EXACTLY the same as:

    for( int id = 0 ; id < ids.length ; id++ )
        out.println( request.getParameter( "empNO" + ids[ id ] ) ) ;


so if you are using 1.5 and putting following lines, it will not work..
    for( int id = 0 ; id < ids.length ; id++ )
out.println( id + " : " + request.getParameter( "empNO" + id ) ) ;

because you are using the counter interger (id) to make your variable name, not the actual value from the array ids.
>>
in 1.5
 for( String id : ids )
        out.println( request.getParameter( "empNO" + id ) ) ;

the variable id is a string whose value matches the individual element of array ids on everu itration...
similar to ids[id]..

in 1.4
  for( int id = 0 ; id < ids.length ; id++ )
id becomes the interger counter & not the string value of elements of ids...

I hope I was able to explain it ...:-)


0
 
LVL 35

Expert Comment

by:TimYates
ID: 17889611
I saw this, and thought it may prove useful and/or interesting :-)

http://today.java.net/pub/a/today/2006/11/07/nuances-of-java-5-for-each-loop.html
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

18 Experts available now in Live!

Get 1:1 Help Now