Reputation: 483
I have a little issue with my code.
I have one dropdown field in my form. I applied onclick event onto it. Onclick, I' m sending ajax call to controller where I'm calling my query to fetch all the list of years and returning it back to blade in response.
My code is working and I can see the ajax response in preview under Network tab but the issue is that the values are not showing on blade into dropdown.
Blade
<div class="form-group">
<select id="regist_year" data-dependent="registration">
<option selected value=""></option>
</select>
</div>
Javascript
$('#regist_year').on('click', function(e){
e.preventDefault();
var _token = $('input[name="_token"]').val();
var all_years = $(this).data('dependent');
$.ajax({
url:"{{ route('getAllyears') }}",
method:"POST",
data:{_token:_token,all_years:all_years},
success:function(result)
{
$('#'+all_years).html(result);
}
});
});
Controller
public function getAllyears(Request $request)
{
$all_years = $request->get('all_years');
$years = DB::table('auto_databases_one')
->select('year')
->distinct()
->orderBy('year', 'ASC')
->get();
$get_years = '<option value="">Select ' . ucfirst($all_years) . '</option>';
foreach ($years as $row) {
$get_years .= '<option value="' . $row->year . '">' . $row->year . '</option>';
}
echo $get_years;
}
Upvotes: 0
Views: 869
Reputation: 17216
well you're putting hyml into $('#'+all_years)
wich is $('#regist_year').data('dependent');
wich leads to $('#registration')
and you have no dom with that ID
change it to
$('#regist_year').html(result);
Also, preventing default on a click on a select might have bad consequences. like dropdown not opening and option not selectable by mouse
Upvotes: 1