ok, first check that your department id is properly send by ajax or not by simply return the only department id, and alert it in success part of ajax ..if it is proper then your are very close to solution..
Apr 25, 2016
5
Level 1
php - Laravel 5.2 cascaded dropdown value not appearing using Ajax
I am building a laravel application where there is a situation of cascading dropdown where user will select a department from list of departments.So when a user select a department teacher will be loaded on that particular data.
I have written all the code. But the problem is after selecting department, instead of loading teacher data, it's showing 'Ok' as it fails.
I don't know why data not get loaded. If anyone find the solution, hope will help to get the solution.
Here is my Route:
Route::get('ajax',function(Request $request){
$department_id = $request->Input(['department_id']);
$teacher=\App\Teacher::where('department_id','=',$department_id)->get();
return Response::json($teacher);
});
View page with ajax:
<div class="container" >
<h3> Create Teacher </h3>
{!! Form::open(array('route' => 'savecourseAssignTeacher','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<div class="form-group">
<label for="">Department</label>
<select class="form-control input-sm" required name="department_id" id="department" >
@foreach($department as $row)
<option value="{{$row->id}}">{{$row->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Teacher</label>
<select class="form-control input-sm" required name="teacher_id" id="teacher" >
<option value=""></option>
</select>
</div>
<button type="submit" class="btn btn-default">Assign</button>
{!! Form::close() !!}
</div>
<script>
$('#department').on('change',function(e){
$('#teacher').find('option').remove().end();
var department_id = $('#department option:selected').attr('value');
var info=$.get("{{url('ajax')}}",{department_id:department_id});
info.done(function(data){
$.each(data,function(index,subcatObj){
$('#teacher').append('<option value="'+subcatObj.id+'">'+
subcatObj.name+'</option>');
});
});
info.fail(function(){
alert('ok');
Please or to participate in this conversation.