I'm working on a dynamic dependent dropdown selection using Ajax
When I select the first column, the second one is always empty and not showing me the list of the city.
Controller code :
public function index () {
$ville=Vil::orderBy('Ville','desc')->get();
return view ('index',['vi'=>$ville ]);
public function delegation ($id)
{
$delegation = DB::table("delegation")
->where("ville_id",$request->ville_id)
->pluck("ville","id");
return response()->json($delegation);
}
My routes:
Route::get('delegation','specvil@deleg');
Route::get('/index','specvil@index');
Jquery code :
<script>
$('select[name="Région"]').on('change', function () {
var countryID = $(this).val();
if (countryID) {
$.get('/deleg/' + countryID, function (data) {
$('select[name="ville"]').empty();
$.each(data, function (key, value) {
$('select[name="ville"]').append('<option value="' + key + '">' + value + '</option>');
});
}, 'json');
} else {
$('select[name="ville"]').empty();
}
}):
});
</script>
The view :
<form action="{{route('ghof')}}" method="get">
{{ csrf_field() }}
<div class="from-box">
<input type="text" class="search-field business" name="rech" value="" placeholder ="Cherchez..">
<select type="text" class="search-field location" name="spec" id="s" value="spec" placeholder ="Spécialités">
<option selected></option>
@foreach($sp as $ss)
<option value=" {{$ss->Spécialité}}"> {{$ss->Spécialité}}</option>
@endforeach
</select>
<select type="text" class="search-field location" name="Région" id="Région" value="Région">
<option selected></option>
@foreach($vi as $vv)
<option value="{{$vv->Ville}}">{{$vv->Ville}}</option>
@endforeach
</select>
<select type="text" class="search-field location" name="ville" id="ville">
</select>
<button class="search-btn" type="submit" id="search"> Recherche </button>
</div>
</form>