Issue
So, this is it: On my JSP page I want to fill a dropdown from a database using a stored procedure that expects as parameter the id
of the country selected in two radios (that is, if you select USA, the States of that country are filled, and if you select Nicaragua, vice versa). As my question says, it is without use of servlets, the problem is that I can not get the value
of the radio selected (if I pass a number instead of the variable "country", the combo is filled).
This is my code:
<label for="state">State</label>
<select name="ddStates" id="ddStates">
<option value="-1">Select State...</option>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nextnetcustomers", "root", "root");
int country = Integer.parseInt(request.getParameter("cntry"));
StringBuilder sb = new StringBuilder();
ResultSet rs = null;
PreparedStatement ps = null;
sb.append("{call SP_CATSTATES_LIST(?)}");
ps = conn.prepareCall(sb.toString());
ps.setInt(1, country);
rs = ps.executeQuery();
while (rs.next()) {
%>
<option value="<%=rs.getInt("IDCATSTATES")%>"><%=rs.getString("DESCRIPTION")%></option>
<%
}
} catch (Exception ex) {
ex.printStackTrace();
out.println("Error " + ex.getMessage());
}
%>
</select>
Solution
Solution: well, finally I had to use a servlet with JSON data and ajax functions, if someone else face this problem, the following is a simple solution:
The ajax functions:
$(document).ready(function () {
$('#USA').change(function (event) {
var id = $("#USA").val();
$.get('PopulateCustomersTable', {
idcontry: id
}, function (response) {
var select = $('#ddStates');
select.find('option').remove();
$.each(response, function (index, value) {
$('<option>').val(value.IdState).text(value.Description).appendTo(select);
});
});
});
$('#NIC').change(function (event) {
var id = $("#NIC").val();
$.get('PopulateCustomersTable', {
idcontry: id
}, function (response) {
var select = $('#ddStates');
select.find('option').remove();
$.each(response, function (index, value) {
$('<option>').val(value.IdState).text(value.Description).appendTo(select);
});
});
});
});
The radios:
<input type="radio" id="USA" name="cntry" value="1"/>USA
<input type="radio" id="NIC" name="cntry" value="2"/>NICARAGUA
And the select dropdown:
<select name="ddStates" id="ddStates" required>
<option value="">Select State...</option>
</select>
Answered By - Chris