SUPERNOVA's avatar

Dropdown dependent

I am building a application where the user will select districts and then choose the villages.

after user selecting a distric, village list displayed ! My problem is a list of villages can not be displayed

maybe I wrong in ajax, If anyones find the solution, I hope will help to get the solution.

images : http://www.awesomescreenshot.com/image/1921416/042bfba084ddde31895f806c19427e13

this my view :

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

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>   

myRoute:

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

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

Route::get('ajax', function(Request $request){
    $kec_id = $request::Input(['kec_id']);
    $villages = \App\Villages::where('kecamatan_id', '=', $kec_id)->get();
    return Response::json($villages);
});

districs model :

protected $table = 'kecamatan';
protected $fillable = ['nama'];

    public function villages()
 {
        return $this->hasMany('App\Villages');
 }

villages model :

protected $table = 'kelurahan';
    protected$fillable = ['nama','kecamatan_id'];

    public function districs()
 {
        return $this->belongsTo('App\Districs');
    }

Migration :

Schema::create('kecamatan', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nama');
        $table->timestamps();
    });

Schema::create('kelurahan', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nama');
        $table->integer('kecamatan_id')->unsigned();
        $table->timestamps();
        $table->foreign('kecamatan_id')->references('id')->on('kecamatan');
    });

end this my problem : http://www.awesomescreenshot.com/image/1921437/ff6f3fd33528f87ee148bbd41c4a4265

0 likes
10 replies
SUPERNOVA's avatar

My 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');
    });
   });

maybe my ajax was wrong

SUPERNOVA's avatar

none of which help, all of them have the same problem -_-

willvincent's avatar

Is that 'public:122:26' text linked to something, in the console.. it looks like it probably links to the line & column of where the syntax error is occuring.

From what I see here -- and no sorry, I'm not going to pour through all of your code -- you need to track down and fix the syntax error(s) in your code, whether or not that resolves the functionality is step 2, but first, fix the syntax errors -- that's what the browser is complaining about.

SUPERNOVA's avatar

can u help me, with my ajax ?

i was try :

 <script type="text/javascript">
    $('#districs').on('change', function(e){
     e.preventDefault();
     var kec_id = $("input[name='kec_id']").val();
     console.log(kec_id);
       $.get('/ajax-vilObj = ' + kec_id, function(data){
        console.log(data);
     });
    });
</script>

but still error :(

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>
1 like

Please or to participate in this conversation.