• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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
0
royalcyber
Asked:
royalcyber
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
TimYatesCommented:
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>
0
 
TimYatesCommented:
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
0
 
royalcyberAuthor Commented:
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>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
                      if( selected_event.getCountry() == (null) || selected_event.getCountry().equals("") )
basically change the above if satement as foliows & where are you setting selected_event????
0
 
TimYatesCommented:
As I said, shouldn't:

                         if( selected_event.getCountry() == (null) && selected_event.getCountry().equals("") )
                         {
                             selected_country = "";
                         }
                         else                                                  
                         {
                             selected_country = "selected";
                         }

be:

                         if( selected_event.getCountry() != null && selected_event.getCountry().equals( next_value ) )
                         {
                             selected_country = "selected";
                         }
                         else                                                  
                         {
                             selected_country = "";
                         }

?
0
 
ManishLeadCommented:
<option value='0'> '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 ) )
                         { %>
  <option value="<%= next_key %>" selected><%= next_value %></option>

                <%  } %>
                         <option value="<%= next_key %>"><%= next_value %></option>
<%                
                    }
%>
               </select>



karan

0
 
TimYatesCommented:
that's what I said with the code shuffled a bit... :-/
0
 
ManishLeadCommented:
sorry..

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

Good luck!

Tim
0
 
ManishLeadCommented:
:)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now