3

I am trying to get dependant select items using ajax call. After selecting 'class' it should show the related 'groups'. But, I am getting 500 internal server error on my console. Would someone help me please to get the expected result? admission-form.blade.php -

<form action="{{ route('admin.students.admission') }}" method="POST" enctype="multipart/form-data">
    {{ csrf_field() }}
<div class="row">
 <div class="col-sm-6">
   <div class="form-group {{ $errors->has('first_admission_class') ? 'has-error' : '' }}">
    <select class="form-control" name="first_admission_class" id="first_admission_class">
        <option value="">Select Class</option>
            @foreach($classes as $class)
                <option value="{{ $class->id }}" {{ (old("first_admission_class") == $class->id ? "selected":"") }}>{{ $class->class_name }}</option>
            @endforeach
    </select>
 </div>
</div>
<div class="col-sm-6">
   <div class="form-group {{ $errors->has('first_admission_class_group') ? 'has-error' : '' }}">
     <select class="form-control" name="first_admission_class_group">

    </select>
    </div>
 </div>
</div>
</form>

Script for Ajax call:

<script>
  $('#first_admission_class').on('change', function(e){
    console.log(e);
    var class_id = e.target.value;

    $.get('http://localhost/school/public/admin/ajax-group/' + class_id, function(data){
      console.log(data);

    });

  });
</script>

web.php -

Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'auth:admin'], function () {
Route::get('ajax-group/{id}', function(){

    $class_id = Input::get('class_id');
    $groups = AvailableclassGroup::where('availableclass_id', '=', $class_id)->get();
    return Response::json($groups);

   });
});
Rashed Hasan
  • 3,414
  • 5
  • 29
  • 71

3 Answers3

2

your route is look like this, when we add param in route they accessible via function param. i hope it works for you.

Route::get('ajax-group/{id}', function($id){
 $groups = AvailableclassGroup::where('availableclass_id', '=', $id)->get();
 return Response::json($groups);
 });
});    

you can check laravel doc Laravel route doc

if still it didnot work then add csrf token, like this in head of your html layout

<meta name="csrf-token" content="{{ csrf_token() }}">

and make your ajax call like this

$.ajaxSetup({
 headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
})


$.ajax({
    type: 'get',
    url: '/ajax-group/'+ class_id,
    dataType: 'json',
    success: function (data) {
    },
    error: function (data) {
     console.log('Error:', data);
    }
});
M Usman Nadeem
  • 383
  • 1
  • 13
0

Your wildcard its named id and you are getting as class_id so change : And make sure your route its named as admin.students.admission

Route::get('ajax-group/{id}', function(){

    $class_id = Input::get('id');
    $groups = AvailableclassGroup::where('availableclass_id', '=', $class_id)->get();
    return Response::json($groups);

   });
})->name('admin.students.admission');

And make sure you have imported the classes on route file.

Leo
  • 6,322
  • 5
  • 18
  • 42
0

as I saw, you're not sending data, so you can't say $class_id = Input::get('id');. You have id parameter in your url, just use it.

Route::get('ajax-group/{id}', function($class_id){

    $groups = AvailableclassGroup::where('availableclass_id', '=', $class_id)->get();
    return Response::json($groups);

});