SUPERNOVA's avatar

Eloquent dropdown dependent

I worked one week for this project, but until now I have not been able to resolve. I am confused :( I want to create a dropdown dependent then displays the data into the table. I have a problem in ajax, in the browser console "SyntaxError: missing (before the formal parameter. I hope someone can help me, thanks a lot :D

view :

{!!Form::open(array('url'=>'', 'files'=>true))!!}
                        <div class="form-group">
                            <label for="title">Pilih Kecamatan :</label>
                                <select class="form-control input-sm" name="districs" id="districs">
                                @foreach ($kecamatan as $districs)
                                    <option value=""{{$districs->id}}>{{$districs->nama}}</option>
                                @endforeach         
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="">Pilih Kelurahan</label>
                                <select class="form-control input-sm" name="districs" id="villages">
                                <option value=""></option>          
                                </select>  
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-default"> Pilih
                            </button>
                        </div>
   {!! Form::close() !!}

ajax :

<script>
        $('#districs').on('change', function(e){
            $('#villages').find('option').remove().end();
               var kec_id = $('#districs option:select').attr('value');
               var info=$.get("{{url('ajax')}}",{kec_id:kec_id});
               info.done(function(data){
                      $each(data,function(index,vilObj){
                      $('#villages').append('<option value="' +vilObj.id+'">'+vilObj.name+'</option>');
                             });
                        });
                info.fail(function){
                alert('ok');
               );
              });
    </script>   

Routes :

Route::get('/', function () {
        $kecamatan = App\districs::all();
     return view('welcome')->with('kecamatan',$kecamatan);
});

Route::auth();

Route::get('/home', 'HomeController@index');

Route::get('/ajax-vilObj', function(){
    $kec_id = Input::get('kec_id');
    $villages = Villages::where('kecamatan_id', '=', $kec_id)->get();
    return Response::json($villages);
});

if someone kindly help me. this my project :

https://drive.google.com/file/d/0B33Uwt7UTSZ5cGV4ekh3REN4dFk/view?usp=sharing

0 likes
2 replies
Snapey's avatar

Not sure what this means

"SyntaxError: missing (before the formal parameter.

What is the actual error?

Also, your route is get('/ajax-vilObj' but the jQuery get is to {{url('ajax')}} ?

SUPERNOVA's avatar

I got it :D

<script type="text/javascript">
    $(document).ready(function() {
        $('select[name="kecamatan"]').on('change', function() {
            var kecamatanID = $(this).val();
         if(kecamatanID) {
                $.ajax({
                        url: 'myform/ajax/'+kecamatanID,
                        type: "GET",
                        dataType: "json",
                        success:function(data) {
                            $('select[name="kelurahan"]').empty();
                            $.each(data, function(key, value) {
                                    $('select[name="kelurahan"]').append('<option 
                    value="'+ key +'">'+ value +'</option>');
                                });
                                }
                        });
                    }else{
                    $('select[name="kelurahan"]').empty();
             }
            });
        });
</script>

Please or to participate in this conversation.