I'm using a dynamic dropdown list in my form
I want to choose an area according to the chosen city
this is my view
<div class="form-group col-md-6 form-group{{ $errors->has('city') ? ' has-error' : '' }}">
<label for="inputPassword4" style="padding-left:35em"><span style="color:red">*</span> city</label>
<select id="city" name="city" class="form-control">
<option ></option>
@foreach($cities as $city)
<option value="{{$city->id}}">{{$city->city}}</option>
@endforeach
</select>
<h6 class="text-danger">{{ $errors->first('city') }}</h6>
</div>
</div>
<div class="form-group col-md-6 form-group{{ $errors->has('area') ? ' has-error' : '' }}">
<label for="inputEmail4" style="padding-left:50em">area</label>
<select id="area" name="area" class="form-control">
<option></option>
</select>
<h6 class="text-danger">{{ $errors->first('area') }}</h6>
</div>
and this is the script
$('#city').on('change',function(e){
console.log(e);
var city= e.target.value;
$.get('/areas/'+ city, function (data){
//console.log(data);
$('#area').empty();
$.each(data,function(i,area){
$('#area').append('<option value ="'+area+'">'+area+'</option>');
});
});
});
</script>e
the controller
public function areas($id)
{
$area = area::where('city_id', $id)->pluck("area","id");
return Response::json($area);
}
routs :
Route::get('/areas/{id}', 'locationController@areas');
i used this code but it didn't work!
Instead of return Response::json($area); use return response()->json($cities);
Your controller will be:
public function areas($id)
{
$area = area::where('city_id', $id)->pluck("area","id");
return response()->json($area);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With