Reputation: 483
I am trying to populate second dropdown based on value selected in first dropdown in Laravel.
When Cinema Hall is selected , it should populate screen dropdown , so I have used here ajax for doing so.
My code is below
HTML code
<div class="form-group">
<label class="control-label col-md-3">Cinema Hall
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
{!! Form::select('cinema_id',$cinemahall,Null,array('class'=>'bs-select form-control','aria-invalid'=>'false','id'=>'cinemahall')) !!}
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Screen
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
{!! Form::select('screen_id',[],Null,array('class'=>'bs-select form-control','aria-invalid'=>'false','id'=>'screenname')) !!}
</div>
</div>
Jquery code
<script>
$(document).ready(function(){
$("#cinemahall").change(function(){
var cinema_id=$("#cinemahall option:selected").val();
//ajax
$.get('/askspidy/admin/showtime/getscreen/' + cinema_id, function(data){
$("#screenname").empty();
// console.log(data);
$.each(data,function(index,screenobj){
$("#screenname").append('<option value="' +screenobj.screen_id + '">' +screenobj.name +'</option>');
});
});
});
});
</script>
Routes
Route::get('/admin/showtime/getscreen/{id}','Admin\ShowtimeController@getscreen');
controller function
public function getscreen($id)
{
$screens=Movies_screen::where('cinema_id',$id)->get();
return response()->json($screens);
}
I checked in Console.log(data) its showing proper data no problem
But dropdown is populating like this
help me to resolve this.
Upvotes: 2
Views: 55
Reputation: 483
ohhh I am so stupid spent hours figuring out the problem, i changed screenobj.name to screenobj.screen_name and it worked...
Upvotes: 0
Reputation: 547
Your should replace screenobj.name
with screenobj.screen_name
as shown in your response preview.
$("#screenname").append('<option value="' +screenobj.screen_id + '">' +screenobj.screen_name +'</option>');
Upvotes: 1
Reputation: 32354
Change name
to screen_name
: screenobj.screen_name
$("#screenname").append('<option value="' +screenobj.screen_id + '">' +screenobj.screen_name +'</option>');
Upvotes: 1