I have a jsp with two drop down location and department.
The value of department dropdown is populated based on the value of location through Ajax.
But when I try to save the page I can’t access the value of department dropdown if the first item from the department drop down is selected.
But if 2nd or 3rd item is selected it can be accessed from the servlet.
<form id="form1" name="crtdocfrm" action="CreateLocation" method="post">
<fieldset width="50%">
<legend>Division</legend>
<table id="table1">
<tr class="tr_stylebutton">
<td>Location Name</td>
<td><select name="locName" onChange="showDept(this.value)">
<option value="-1">--select--</option>
<%
Iterator itrLocation = arlLocation.iterator();
while (itrLocation.hasNext()) {
%>
<option value=<%=itrLocation.next()%>><%=itrLocation.next()%>
</option>
<%}%>
</select></td>
</tr>
<tr class="tr_stylebutton">
<td>Department Name</td>
<td>
<select name="ddDeptName" onChange="alert(this.value)"></select>
</td>
</tr>
<tr class="tr_stylebutton">
<td>Division ID</td>
<%
LocationPolulate lp = new LocationPolulate();
int Div_Id = lp.DivisionId();
%>
<td><input type="text" name="divID" disabled="true" value=<%=Div_Id + 1%>/></td>
</tr>
<tr class="tr_stylebutton">
<td>Division Name</td>
<td><input type="text" name="divName" value=""/></td>
</tr>
</table>
<table border="0" width="55%" align="left" id="table-button">
<tr class="tr_stylebutton">
<td align="right"><input type="submit" value="Save" class="button"/></td>
<td align="left"><input type="submit" value="Cancel" class="button"/></td>
</tr>
</table>
</fieldset>
<br/>
</form>
Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
int intLocId = Integer.parseInt(request.getParameter("locName"));
String strDivNm = (String) request.getParameter("divName");
int intDeptId = Integer.parseInt(request.getParameter("ddDeptName"));
LocationPolulate lp = new LocationPolulate();
int Res = lp.InsertDivision(strDivNm, intLocId, intDeptId);
if (Res == 0) {
request.setAttribute("Errmsg", "Error Cannot Save Division");
RequestDispatcher dis1 = getServletContext().getRequestDispatcher("/AddDivision");
dis1.forward(request, response);
} else {
int Div = lp.DivisionId();
request.setAttribute("succmsg", "Successfully saved Division with division id:" + Div);
RequestDispatcher dis1 = getServletContext().getRequestDispatcher("AddDivision.jsp");
dis1.forward(request, response);
}
}
Any ideas?
sorry for adding code as a part off comment…i could not fit the whole code so i had to add the comment..there was a mistake in the java script.the correct one is given below.Thanks all for your suggestion …
i am getting the id and name in the same string from another jsp and spliting it to get the value of the different department id and name.