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,036 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
[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
  • 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
Technology Partners: 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 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
 
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

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

The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

707 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