473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 23215
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

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

Similar topics

1
by: Laphan | last post by:
Hi All Sorry for the confusing subject, but I'm hoping that you will be able to help. In very basic terms I want to emulate a <SELECT> HTML box in VB. All I want to do is display a title...
5
by: Elvis V. | last post by:
Good morning, I have a table with three fields, Buildings, Floors and Rooms. This is what I would like to do; in my form when I select Building1 in my drop down box for Buildings, when I go to the...
3
by: John Walker | last post by:
Hi, On an ASP.NET page I have a drop down list control. When the user pulls down the list and makes a selection, I perform validation, and if the validation fails I want the selected item in...
1
by: rich2912 | last post by:
I have a page, with three drop down boxes. Each has four choices linked to a database. Here is what I would like to do: I would like the results of the three choices, to be displayed on the...
1
by: tsunethere | last post by:
hi .. I have 2 drop-down boxes. at first when web page will get load value this drop-down value will be"---" in this fashion. and 2nd drop-down will be disabled. unless and until user selects...
8
by: bimeldip | last post by:
Hi, i would like to display the list of tables in a database in a drop down list then when user selects a table, the table will be dispalyed on the page. So far i've done this: <? $dbname =...
1
by: runway27 | last post by:
hi i have registration form where user selects from a drop down to select their area code apart from filling other details in the form. in the next page which is a confirmation page i would like...
7
Chrisjc
by: Chrisjc | last post by:
have one page that has a drop down menu, once the user selects the desired option it will then pass the selected value to a 2nd drop down. Once they select there next option I pass that value... Now...
4
by: bonneylake | last post by:
Hey Everyone, Well this is probably a pretty easy question, but for some reason i just can not figure it out. What i am trying to do is redisplay what was entered previously. I have figured...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.