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_i ndex];
String next_value = (String)countries.get(next _key);
String selected_country = "";
if(selected_event.getCount ry() == (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
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_i
String next_value = (String)countries.get(next
String selected_country = "";
if(selected_event.getCount
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
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
if( selected_event.getCountry(
that I changed...
It probably should be:
if( selected_event.getCountry(
But you don't say exactly what selected_event is...
Tim
ASKER
I have actually stored the countries in a LinkedHashMap
LinkedHashMap countries = (LinkedHashMap)request.get Attribute( "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_i ndex];
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>
LinkedHashMap countries = (LinkedHashMap)request.get
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_i
String next_value = (String)countries.get(next
String selected_country = "";
if( selected_event.getCountry(
{
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????
basically change the above if satement as foliows & where are you setting selected_event????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..!
% } else %>
<option value="<%= next_key %>"><%= next_value %></option>
<%
}
%>
Ya tim , u r right..!
ASKER
Thank you very much guys
Glad to be of help :-)
Good luck!
Tim
Good luck!
Tim
:)
<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_i
String next_value = (String)countries.get(next
String selected_country = "";
if( selected_event.getCountry(
{
selected_country = "selected";
}
else
{
selected_country = "";
}
%>
<option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%
}
%>
</select>
</td>