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,032 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
issue with pdf generation 2 88
difference between member and local variables. 29 134
SimpleDateFormat.parse examples 19 94
listing all functions in JavaScript 19 202
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Google always has something new and amazing up its sleeve, and the most current thing that they have been working on is another step in the evolution of Google Search, from machine learning to its brilliant successor, deep learning.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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