I have a populated dropdown list in my view and when i choose one date from the list I want to fill another table with data from the database.
I created a controller method called description that takes too parameters as seen below, which returns the data that I need in JSON format
When I manualy enter the url I get the correct json response but when I try to pass the url using Ajax it does not work.
My Controller Code
public function description($car,$date)
{
$description = DB::table("histories")
->where('crn',$car)
->where("date",$date)
->get();
return json_encode($description);
}
My Route
Route::get('admin/cars/history/{car}/{date}','CarController@description');
My view
<div class="container-fluid">
<ol class="breadcrumb">
<li><a href="{{url('admin')}}">Home</a></li>
<li><a href="{{url('admin/cars')}}">Car</a></li>
<li class="active">{{$car->crn}}</li>
</ol>
<div class="box box-success">
<div class="panel">
<div class="panel-heading"><h3>Car Details</h3></div>
<div class="box-header with-border">
<div class="form-group">
<label for="owner">Owner</label>
<p>{{$car->customer->name}}</p></div>
<div class="form-group">
<label for="manufacturer">Manufacturer</label>
<p>{{$car->manufacturer}}</p></div>
<div class="form-group">
<label for="model">Model</label>
<p>{{$car->model}}</p></div>
<div class="form-group">
<label for="address">Last Visit</label>
<p>{{$lastservice->created_at->toFormattedDateString()}}</p></div>
<div class="form-group">
<label for="">Select Date to view history records</label>
<select name="date" class="form-control" style="width:250px">
<option value="">--- Select Date ---</option>
@foreach ($history as $date => $value)
<option value="{{ $date }}">{{ $value }}</option>
@endforeach
</select>
<a href="{{url('admin/cars/')}}" class="btn btn-default">Back</a></div>
</div>
</div>
My JQuery
<script type="text/javascript">
$(document).ready(function() {
var results = $('#results');
$('select[name="date"]').on('change', function() {
var $date = $('option:selected').text();
$.ajax({
url: 'admin/cars/history/HNH419/'+$date,
type: "GET",
dataType: "json",
success:function(response){
alert(response);
}})
})
});
</script>
