469,277 Members | 2,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

Displaying the selected value from the drop down in the next page.

283 100+
Hello all,
Im making a dependent drop down for my application for filtering purpose. I have populated the drop down from the database and retrieved in the html.
[HTML]
<tr>
<td valign="top" width="138" height="26"><small><font face="Verdana"><strong>Branch</strong></font></small></td>
<td width="379" height="26">
<select name="branch" size="1" onChange="messValue()">
<option value="">(select branch)</option>
<%
publicity.Pub1Form pfObj = null;
ArrayList list =(ArrayList)request.getAttribute("list");
for(Iterator itr = list.iterator(); itr.hasNext();){
pfObj=(Pub1Form)itr.next();{

%>

<option value="<%=pfObj.getDescription() %>"><%=pfObj.getDescription() %></option>
<%}} %>
<option value="">-------------------------------------------------------</option>

</select>
<%session.setAttribute("list",list); %>
</td>
</tr>
[/HTML]

This is the arraylist from where i populate the select. My generated page looks like this..
[HTML]
<select name="branch" size="1" onChange="messValue()">
<option value="">(select branch)</option>


<option value="Bombay">Bombay</option>


<option value="Delhi">Delhi</option>


<option value="Ahmedabad">Ahmedabad</option>

and so on....
[/HTML]

this is in my a.jsp(for example).
The javascript ive written is..

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.         function messValue(){
  3. var messageIndex = document.arch.branch.selectedIndex;
  4. var selectedValue = document.arch.branch.options[messageIndex].value;
  5. document.arch.branch.value = selectedValue;
  6. document.arch.submit();
  7. }
  8. </script>
  9.  
When i select any particular value, say bombay, the onchange fires and submits, and my b.jsp page populates. All this works fine, but the selected value from the drop down of the a.jsp page should be shown as selected in the b.jsp page, this is not working out..How would i do some thing like this. I hope im clear in expressing my problem.

regards,

ajos
Mar 25 '08 #1
15 22729
pshm
20
hi,
you need to use session or cookie to carry the value from one page to another page.
In jsp you can better use the session
in the first page set the value which was selected by the drop down
Expand|Select|Wrap|Line Numbers
  1. <%
  2.    session.setAttribute( "theName", name );
  3. %>
  4.  
and you can retrive this from the next page
Expand|Select|Wrap|Line Numbers
  1. <%= session.getAttribute( "theName" ) %>
  2.  
Mar 25 '08 #2
ajos
283 100+
hi,
you need to use session or cookie to carry the value from one page to another page.
In jsp you can better use the session
in the first page set the value which was selected by the drop down
Expand|Select|Wrap|Line Numbers
  1. <%
  2.    session.setAttribute( "theName", name );
  3. %>
  4.  
and you can retrive this from the next page
Expand|Select|Wrap|Line Numbers
  1. <%= session.getAttribute( "theName" ) %>
  2.  
Thanks for the quick reply.Yup thats what im doing. This part is not the problem. The problem is when i click on the drop down box the 2nd drop down gets populated but the selected value is always the first value. My question is what js modification do i have to do to show the selected value from a.jsp as selected in b.jsp. I hope im clear here?

in b.jsp the source generated is
[HTML]<select name="branch" size="1">

<option value="Bombay" true>Bombay</option>

<option value="Delhi" true>Delhi</option>

<option value="Ahmedabad" true>Ahmedabad</option>[/HTML]
How would i show the selected value?

regards.
Mar 25 '08 #3
hsriat
1,654 Expert 1GB
Thanks for the quick reply.Yup thats what im doing. This part is not the problem. The problem is when i click on the drop down box the 2nd drop down gets populated but the selected value is always the first value. My question is what js modification do i have to do to show the selected value from a.jsp as selected in b.jsp. I hope im clear here?

in b.jsp the source generated is
[HTML]<select name="branch" size="1">

<option value="Bombay" true>Bombay</option>

<option value="Delhi" true>Delhi</option>

<option value="Ahmedabad" true>Ahmedabad</option>[/HTML]
How would i show the selected value?

regards.
If it were PHP, I would have done something like this.
[php]<?php
$selected = array("Bombay"=>"", "Delhi"=>"", "Ahmedabad"=>"");
$selected[$_POST['branch']] = "selected";
?>
<select name="branch" size="1">
<option value="Bombay" <?php echo $selected['Bombay']; ?>>Bombay</option>
<option value="Delhi" <?php echo $selected['Delhi']; ?>>Delhi</option>
<option value="Ahmedabad" <?php echo $selected['Ahmedabad']; ?>>Ahmedabad</option>
</select>[/php]Hope you could understand.
Mar 25 '08 #4
ajos
283 100+
If it were PHP, I would have done something like this.
[php]<?php
$selected = array("Bombay"=>"", "Delhi"=>"", "Ahmedabad"=>"");
$selected[$_POST['branch']] = "selected";
?>
<select name="branch" size="1">
<option value="Bombay" <?php echo $selected['Bombay']; ?>>Bombay</option>
<option value="Delhi" <?php echo $selected['Delhi']; ?>>Delhi</option>
<option value="Ahmedabad" <?php echo $selected['Ahmedabad']; ?>>Ahmedabad</option>
</select>[/php]Hope you could understand.
Hey hsriat,
Thanks for your inputs, but honestly php seems chinese to me :), but i got your point here coz ive done this for hard coded values, the values here come from the database.

regards,
ajos
Mar 25 '08 #5
hsriat
1,654 Expert 1GB
Hey hsriat,
Thanks for your inputs, but honestly php seems chinese to me :), but i got your point here coz ive done this for hard coded values, the values here come from the database.

regards,
ajos
In short, I meant that you would need to add selected attribute to the option in the next page, which was selected in the previous page.

And I just came to know few days ago that there is no actual language named Chinese. Its either Mandarin or Cantonese. To us (non-Chinese people), both of them sound similar, so we call it Chinese. ;)

Regards,
Harpreet
Mar 25 '08 #6
ajos
283 100+
In short, I meant that you would need to add selected attribute to the option in the next page, which was selected in the previous page.
I perfectly understand what you mean to say here by "adding the selected attribute". The way im populating the dropdown for the b.jsp is by setting the arraylist data of a.jsp in session scope and retrieving it in b.jsp by doing something like this.

Expand|Select|Wrap|Line Numbers
  1. <%session.getAttribute("list"); %>
  2.   <tr>
  3.     <td valign="top" width="138" height="26"><small><font face="Verdana"><strong>Branch</strong></font></small></td>
  4.     <td width="379" height="26">
  5.     <select name="branch" size="1">
  6.     <%Pub1Form pfObj = null;
  7.       ArrayList list1 = (ArrayList)session.getAttribute("list");
  8.       for(Iterator it = list1.iterator();it.hasNext();){
  9.         pfObj=(Pub1Form)it.next();{
  10.           %>
  11.  
  12.      <option selected value="<%=pfObj.getDescription() %>" <%=branch.equals(branch)%>><%=pfObj.getDescription() %></option>
  13.       <%}} %>
  14.       <option value="">-------------------------------------------------------</option>
  15.  
  16.     </select>
  17.     </td>
  18.   </tr>
  19.  
And if you notice i have a loop going on, and inside the loop the <option> is present, on placing the selected attribute in the option, all the values that come from the db get set as selected....
[HTML]
<select name="branch" size="1">


<option selected value="Bombay" true>Bombay</option>


<option selected value="Delhi" true>Delhi</option>


<option selected value="Ahmedabad" true>Ahmedabad</option>
[/HTML]
Ultimately the last value is getting selected by default. Hope im clear somewhat. Im in a fix here.


And I just came to know few days ago that there is no actual language named Chinese. Its either Mandarin or Cantonese. To us (non-Chinese people), both of them sound similar, so we call it Chinese. ;)

Regards,
Harpreet
lol, mandarin or cantonese they all sound chinese to me (no offence meant to anybody)

regards,

ajos
Mar 25 '08 #7
pshm
20
hi try this with your jsp.. it may help you

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function init(){
  4.                   document.getElementById('branch').value = '<%= session.getAttribute( "theName" ) %>';
  5. }
  6. //-->
  7. </script>
  8. <body onload="init();">
  9. <select name="branch" id="branch" size="1">    
  10.     <option value="Bombay" true>Bombay</option>
  11.  
  12.     <option value="Delhi" true>Delhi</option>
  13.  
  14.     <option value="Ahmedabad" true>Ahmedabad</option>
  15. </select>
  16.  
  17. </body>
  18.  
regards,
Mar 25 '08 #8
hsriat
1,654 Expert 1GB
[html]<option value="<%=pfObj.getDescription() %>" <% if (session.getAttribute("list").equals(pfObj.getDesc ription())) print('selected');%>><%=pfObj.getDescription() %></option>[/html]
print('selected') .... use whatever is the right syntax in jsp.
Mar 25 '08 #9
ajos
283 100+
hi try this with your jsp.. it may help you

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function init(){
  4.                   document.getElementById('branch').value = '<%= session.getAttribute( "theName" ) %>';
  5. }
  6. //-->
  7. </script>
  8. <body onload="init();">
  9. <select name="branch" id="branch" size="1">    
  10.     <option value="Bombay" true>Bombay</option>
  11.  
  12.     <option value="Delhi" true>Delhi</option>
  13.  
  14.     <option value="Ahmedabad" true>Ahmedabad</option>
  15. </select>
  16.  
  17. </body>
  18.  
regards,
Ok this is a very close solution, but not quite coz
1) i tried the code and when i selected the option i didnt get the option as selected instead i got a blank select.
2) i cant use onload since i already use that function to append the date.(Can i use 2 onloads on the body tag?).

The real problem here is the arraylist data,session and the loops.
Mar 25 '08 #10
ajos
283 100+
[html]<option value="<%=pfObj.getDescription() %>" <% if (session.getAttribute("list").equals(pfObj.getDesc ription())) print('selected');%>><%=pfObj.getDescription() %></option>[/html]
print('selected') .... use whatever is the right syntax in jsp.
Ok. I even tried that but still getting the first value in the list.
The way i did it is...

[HTML]
<option value="<%=pfObj.getDescription() %>" <%=session.getAttribute("list").equals(pfObj.getDe scription()) ? "selected" : ""%>><%=pfObj.getDescription() %></option>
[/HTML]
Mar 25 '08 #11
hsriat
1,654 Expert 1GB
Ok, tell me one thing first.
How do you retrieve posted data from a form to next page?

If you can come to know what was the selected value in the last page, compare that inside the loop with your pfObj.getDescription(). If equal, display selected, else nothing.
Expand|Select|Wrap|Line Numbers
  1. <%=session.getAttribute("list").equals(pfObj.getDescription()) ? "selected" : ""%>
should be working if session.getAttribute("list") gives you the value selected in last page.
Mar 25 '08 #12
ajos
283 100+
Ok, tell me one thing first.
How do you retrieve posted data from a form to next page?

If you can come to know what was the selected value in the last page, compare that inside the loop with your pfObj.getDescription(). If equal, display selected, else nothing.
Expand|Select|Wrap|Line Numbers
  1. <%=session.getAttribute("list").equals(pfObj.getDescription()) ? "selected" : ""%>
should be working if session.getAttribute("list") gives you the value selected in last page.
Ok hsriat it works great now, i changed the session.getAttribute to request.getParameter("branch").
[HTML] <option value="<%=pfObj.getDescription() %>" <%=request.getParameter("branch").equals(pfObj.get Description()) ? "selected" : ""%>><%=pfObj.getDescription() %></option>[/HTML]

and it worked great.
Well thanks harpreet for the great support even though we had the chinese/english difference between us:), if you know what i mean(read php/jsp) you really helped me, wasnt possible without you. Thanks again. :)

regards,

ajos
Mar 25 '08 #13
pshm
20
Ok this is a very close solution, but not quite coz
1) i tried the code and when i selected the option i didnt get the option as selected instead i got a blank select.
2) i cant use onload since i already use that function to append the date.(Can i use 2 onloads on the body tag?).

The real problem here is the arraylist data,session and the loops.
you can call this function init() in your append function also.
or you can add with in the script tag after the init(0 funtion definition
Expand|Select|Wrap|Line Numbers
  1. window.document.onload = init();
  2.  
the value wat'er u r passing from a.jsp is present in b.jsp surely it will get reflect in b.jsp otherwise check the values in b.jsp
Mar 25 '08 #14
ajos
283 100+
you can call this function init() in your append function also.
or you can add with in the script tag after the init(0 funtion definition
Expand|Select|Wrap|Line Numbers
  1. window.document.onload = init();
  2.  
the value wat'er u r passing from a.jsp is present in b.jsp surely it will get reflect in b.jsp otherwise check the values in b.jsp
Hey pshm, i got it working finally thanks for your input as well, i really appreciate the time you guys have taken.

regards,
ajos
Mar 25 '08 #15
hsriat
1,654 Expert 1GB
Ok hsriat it works great now, i changed the session.getAttribute to request.getParameter("branch").
[HTML] <option value="<%=pfObj.getDescription() %>" <%=request.getParameter("branch").equals(pfObj.get Description()) ? "selected" : ""%>><%=pfObj.getDescription() %></option>[/HTML]

and it worked great.
Well thanks harpreet for the great support even though we had the chinese/english difference between us:), if you know what i mean(read php/jsp) you really helped me, wasnt possible without you. Thanks again. :)

regards,

ajos
Yeah, it should be request.getParameter("branch"), but I had no idea about jsp.
But I'm glad to know you got it working finally.

Regards,
Harpreet.
Mar 25 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by John Walker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.