Link to home
Start Free TrialLog in
Avatar of royalcyber
royalcyber

asked on

how to show a specific value in an option

Hi I am using an option in order to help the user to select a particular country. What I need is that if a user needs to edit a specific record the option should show the country name which the user had entered before

Here is my code

<td>
                  <select name="COUNTRY" class="textfield">
                  
            <option value='0' selected> 'Please select one'</option>
<%
                        for(int key_index = 0; key_index < country_keys.length; key_index++) {
                              String next_key   = (String)country_keys[key_index];
                              String next_value = (String)countries.get(next_key);
                              String selected_country = "";
                              if(selected_event.getCountry() == (null) || selected_event.getCountry() == "") { selected_country = "selected"; }
                              else                                                   { selected_country = "";}
%>
                              <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                        } %>
                  </select>
            </td>

In this example if there is a value for selected_event.getCountry than the option should show that value, in other words it should be selected. Right now it shows the last country by default

Any help will be greatly appreciated
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

As a pure guess:

    <td>
       <select name="COUNTRY" class="textfield">
               
          <option value='0' selected> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )
                         {
                             selected_country = "selected";
                         }
                         else                                                  
                         {
                             selected_country = "";
                         }
%>
                         <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                
                    }
%>
               </select>
          </td>
it was this line:

                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )

that I changed...

It probably should be:

                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_key ) )

But you don't say exactly what selected_event is...

Tim
Avatar of royalcyber
royalcyber

ASKER

I have actually stored the countries in a LinkedHashMap
 
LinkedHashMap countries = (LinkedHashMap)request.getAttribute("COUNTRIES");

next_key = key of hashmap
next_value = value of hashmap

Sorry it was a typo the real code was like this

    <td>
       <select name="COUNTRY" class="textfield">
               
          <option value='0' selected> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() == (null) && selected_event.getCountry().equals("") )
                         {
                             selected_country = "";
                         }
                         else                                                  
                         {
                             selected_country = "selected";
                         }
%>
                         <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                
                    }
%>
               </select>
          </td>
                      if( selected_event.getCountry() == (null) || selected_event.getCountry().equals("") )
basically change the above if satement as foliows & where are you setting selected_event????
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that's what I said with the code shuffled a bit... :-/
sorry..

%  } else %>
           <option value="<%= next_key %>"><%= next_value %></option>
<%                
              }
%>
Ya tim , u r right..!
Thank you very much guys
Glad to be of help :-)

Good luck!

Tim
:)