Rhokai
Rhokai

Reputation: 373

Dynamic Drop down using Laravel

I m trying to get a dynamic dropdown menu using another dropdown. here this is my blade file.

<div class="form-group">
{!! Form::label('ItemID', 'Code:') !!}
<select class="form-control input-sm" name="ItemID" id="ItemID">
@foreach($items as $itm)
<option value="{{$itm->ID}}">{{$itm->Code}}</option>
@endforeach  
</select>
</div>


<div class="form-group">
{!! Form::label('ActivityItemsID', 'Activity:') !!}
<select class="form-control input-sm" name="ActivityItemsID" id="ActivityItems">
<option value=""></option>

</select>
</div>

my first dropdown works fine.
This is my route.php

Route::get('/addschedule',function(){
$itemID = Input::get('ItemID');
$sub = DB::table('ActivityItem')->where('ItemID','=',$itemID)->get();
return $sub;
});


This is the script I used.

<script>

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

$('#ItemID').on('change', function(e){
    console.log(e);
    var itemID = e.target.value;

    $.get('{{ url('information') }}/addschedule?itemID=' + itemID, function(data) {
        console.log(data);
        $('#ActivityItems').empty();
        $.each(data, function(index,subCatObj){
            $('#ActivityItems').append(''+subCatObj.name+'');
        });
    });
});

});
</script>

When I try this, I get

Uncaught SyntaxError: Unexpected identifier

What is the problem with my code? I am using Laravel 5.2 and Mysql.
Thanks in advance.

Upvotes: 0

Views: 311

Answers (1)

David Smith
David Smith

Reputation: 603

You have an error in your JavaScript. You haven't closed the function call to $.ajaxSetup() correctly. Your code should look more like this:

<script>

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

$('#ItemID').on('change', function(e) {
     console.log(e);
     var itemID = e.target.value;

     $.get('{{ url('information') }}/addschedule?itemID=' + itemID, function(data) {
        console.log(data);
        $('#ActivityItems').empty();
        $.each(data, function(index,subCatObj){
            $('#ActivityItems').append(''+subCatObj.name+'');
        });
     });
});

Upvotes: 1

Related Questions