Hi,
I have 2 select/dropdown fields in my form. Field2 is dependent on the selected values of Field1, and I am pulling the values of Field2 via Javascript/Ajax URL by feeding it the value of Field1 when selected.
<div class="col-sm-3">
<select name="ministry" class="form-control">
<option value="">All ministries</option>
@foreach ($mins as $min)
<option value="{{ $min->id }}" @if($min->id==$ministry) selected @endif>{{ $min->name }}</option>
@endforeach
</select>
<span class="help-block">Ministry</span>
</div>
<div class="col-sm-3">
<select name="department" id="department" class="form-control">
</select>
<span class="help-block">Select Department</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('select[name="ministry"]').on('change', function() {
var mhID = $(this).val();
if(mhID) {
$.ajax({
url: 'admin/departmentajax/'+mhID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="department"]').empty();
$('select[name="department"]').append('<option value="">Select Department...</option>');
$.each(data, function(key, value) {
$('select[name="department]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="department"]').empty();
}
});
});
</script>
The problem is that after the form submission, the ministry value is pre-selected in the dropdown (logically fine, since the result is related to the selected ministry), but the department value is not populated with the requested value, it is always blank.
Any help please?
Thanks,
Saad