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,034 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
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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
JSP Popup page 2 137
dateFashion java challenge 9 146
dynamic reloading of jsp in jetty 2 121
struts hibernate integration example 28 143
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Riverbed Technology's webinar discusses networking for the cloud era with simplified SD-WAN cloud connectivity.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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