• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 70
  • Last Modified:

How to retain the dropdown value after submitting the form

I had this question after viewing Retain the value of Drop down box in jsp.
I tried the below code but i'm getting error : alloc_Type is undefined on refresh.
<%
 String riskTypeValue = request.getParameter("riskType");
if(riskTypeValue == null)riskTypeValue = "0";

String allocTypeValue = request.getParameter("allocType");
if(allocTypeValue == null)allocTypeValue = "0";
%>

<script>
function retainValue(){
      //alert("retainValue "+alloc_Type +" && " +risk_Type);
    var element1 = document.forms[0].allocType;
    for(x = 0; x < element1.length; x++){
            if(element1.options[x].value == "<%=alloc_Type%>")
            {
                  element1.options[x].selected = true;
                  break;
            }
    }
    var element2 = document.forms[0].riskType;
    for(x = 0; x < element2.length; x++){
            if(element2.options[x].value == "<%=risk_Type%>"){
                  element2.options[x].selected = true;
                  break;
            }
    }
}
</script>


<tr>
                        <td>&nbsp;</td>
                        <td class="smalltext" width="150px"><b>Allocation Type:</b></td>
                        <td class="smalltext">
                               <select id="allocType"
                                          class="filter_smalltext" name="allocType">
                                    <c:forEach items="${dataBO.alloc_types}" var="allocTypes">
                                          <option value="${allocTypes.key}" ${ allocTypes.key eq dataBO.alloc_type ? 'selected':'' }>
                                                <c:out value="${allocTypes.value}"></c:out>
                                          </option>
                                    </c:forEach>
                              </select>
                        </td>
                        <td>&nbsp;</td>
                  </tr>
0
Sneha Raju
Asked:
Sneha Raju
  • 2
1 Solution
 
Olaf DoschkeSoftware DeveloperCommented:
if(element1.options[x].value == "<%=alloc_Type%>")

Every other line of code is talking of allocType and allocTypes without underscore, why should <%=alloc_Type%> work, if no previousline defines something like that?

In one of the first lines you define: String allocTypeValue = request.getParameter("allocType") so I assume you would need to use that name: allocTypeValue or <%=allocTypeValue%>, as allocTypeValue is defined.

Bye, Olaf.
0
 
rrzCommented:
Did you change from your original question?  
I wrote a demonstration code.  If you are using JSTL, then I can rewrite it.
<%
    String cr = "";
	String mr = "";
	String both = "";
	String none = "";
	String selectedString = "selected=\"selected\"";
	String selectedValue = request.getParameter("riskType");
	if(selectedValue == null)none = selectedString;
	if("CR".equals(selectedValue))cr = selectedString;
	if("MR".equals(selectedValue))mr = selectedString;
	if("BOTH".equals(selectedValue))both = selectedString;
%>
<html>
<head>
</head>
<body>
	<form>
		<select name="riskType">
            <option value="" <%=none%>>Choose One</option>
            <option value="CR" <%=cr%>>CR</option>
            <option value="MR" <%=mr%>>MR</option>
            <option value="BOTH" <%=both%>>BOTH</option>
        </select>
		<input type='submit' value='Submit'/>
    </form>
	Selected value is <%=selectedValue%>
</body>
</html>

Open in new window

0
 
rrzCommented:
I believe I answered the question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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