I would like to create a dynamic dropdown list dependent based on module code. The dependent dropdown list should show the available class for selected modules. There are 2 tables one is called modules and mupp_classes. the Models are connected to a controller for each table. module_code is the FK for mupp_classes. The column for the classes is called name in mupp_classes below are the code which I currently have.
my javascript
<script>
$('#module_code').on('change', function(e){
console.log(e);
var module_code = e.target.value;
$.get('admin.ajaxFunction' + module_code, function(data) {
console.log(data);
$('#mupp_classes-name').empty();
$.each(data, function(index,subCatObj){
$('#mupp_classes-name').append(''+subCatObj.name+'');
});
});
});
my route
Route::get('ajax-state','ClassController@ajaxFuction')->name('ajaxFunction');
My controller
public function ajaxFunction()
{
$code = Input::get('module_code');
$subcategories = mupp_Class::where('module_code','=',$code)->get();
return $subcategories;
}
my view
<div class="form-group">
<label for="modulecode">Module code</label>
<select id="module_code" class="form-control" name="module_code">
<option value="">Select Module Code</option>
@foreach ($moduleslist as $module)
<option value="">
</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="mupp_classes-name">Module Class</label>
<select id="mupp_classes-name" class="form-control input-sm"
name="mupp_classes-name">
<option value=""></option>
</select>
</div>
via Wei Jun