dekathari
asked on
Retain selected value in combo box after page getting refreshed
I have a jsp with a combo box with these options.
<select name="category">
<option value="Shopping">Shopping< /option>
<option value="Sports" >Sports</option>
<option value="Travel">Travel</opt ion>
</select>
After I select any of these options, I need to refresh the page, but the problem is that the selection is not getting saved after refrsh. How do I retain the selection after refresh?
<select name="category">
<option value="Shopping">Shopping<
<option value="Sports" >Sports</option>
<option value="Travel">Travel</opt
</select>
After I select any of these options, I need to refresh the page, but the problem is that the selection is not getting saved after refrsh. How do I retain the selection after refresh?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is what I tried, but I am getting an exception
Duplicate local variable category
If I change the category variable to some other name, the error is gone, but the selecttion is not retained. Am I missingg something?
<select name="category" onChange="myForm.submit(); ">
<option value="Shopping">Shopping< /option>
<option value="Sports" >Sports</option>
<option value="Travel">Travel</opt ion>
</select>
<%
String category=request.getParame ter("categ ory");
if(category.equals("Shoppi ng")){ %>
<option value="Shopping" selected>Shopping</option>
<% } else {%>
<option value="Shopping">Shopping< /option>
<%}
%>
Duplicate local variable category
If I change the category variable to some other name, the error is gone, but the selecttion is not retained. Am I missingg something?
<select name="category" onChange="myForm.submit();
<option value="Shopping">Shopping<
<option value="Sports" >Sports</option>
<option value="Travel">Travel</opt
</select>
<%
String category=request.getParame
if(category.equals("Shoppi
<option value="Shopping" selected>Shopping</option>
<% } else {%>
<option value="Shopping">Shopping<
<%}
%>
ASKER
Thanks a lot.
I have found the solution, this is what I did and is working fine.
<script>
function messageValue()
{
document.myForm.submit();
}
</script>
<%
String selected = (String)request.getParamet er("catego ry");
if(selected == null)
selected = "Shopping"; // default value
%>
<select name="category" onchange="messageValue()">
<option value="Shopping" <%=selected.equals("Shoppi ng")?"sele cted":""%> > Shopping</option>
<option value="Sports" <%=selected.equals("Sports ")?"select ed":""%> > Sports</option>
<option value="Travel" <%=selected.equals("Travel ")?"select ed":""%> > Travel</option>
</select>
I have found the solution, this is what I did and is working fine.
<script>
function messageValue()
{
document.myForm.submit();
}
</script>
<%
String selected = (String)request.getParamet
if(selected == null)
selected = "Shopping"; // default value
%>
<select name="category" onchange="messageValue()">
<option value="Shopping" <%=selected.equals("Shoppi
<option value="Sports" <%=selected.equals("Sports
<option value="Travel" <%=selected.equals("Travel
</select>
if(category.equals("Shoppi
<option value="Shopping" selected>Shopping</option>
<% } else {%>
<option value="Shopping">Shopping<
<%} %>