I have two views, first view is use to collect user input ( user select site and date) the second view is pdf view(will show in browser). The problem now is when user select the input(site and date) from first view and click on a button called "convert button", controller and second view(PDF) cannot get the data.
*In first view i have 2 buttons,
1) filter button: filter input data then return data back to the first view (query works and able to get the data i want)
2)convert button: filter input data then return data back to second view [pdf view] -->this one not working
first view HTML
<select name="sites" id="sites" required="required" style="border:1px solid black; margin-left:10px; width:200px; padding:3px; font-size:16px; text-align-last:center;" >
<option value="" selected="" disabled > </option>
<option value="ABC">ABC</option>
<option value="BBC">BBC</option>
<option value="BBE">BBE</option>
<option value="QWE">QWE</option>
</select>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label style="margin-left:20px; font-size:16px;">Start Date : </label>
<input type = "text" name="startdate" id = "datepicker-8" style="border:1px solid black; width:270px;margin-left:47px; text-align:center; font-size:16px;" placeholder="Select Start Date" autocomplete="off" required="required" />
</div>
</div>
<div class='col-md-6'>
<div class="form-group">
<label style="margin-left:20px;font-size:16px;">End Date : </label>
<input type = "text" name="enddate" id = "datepicker-9" style="width:270px; margin-left:53px; text-align:center; font-size:16px;" placeholder=" End Date" disabled />
</div>
</div>
</div>
convert button:
<div class="button" style="bottom:-25px">
<button class="btn btn-success" id="convertBtn" name="convertBtn" type="button" style="width:150px; margin-left:60px; font-size:16px;" onclick="location.href = '/home/billing/pdf';" > Convert to PDF</button>
</div>
</div>
ajax that i used to pass data to controller
$('#convertBtn').click(function() {
var e = document.getElementById("sites");
var site = e.options[e.selectedIndex].text;
var startdate = $('#datepicker-8').val();
var enddate = $('#datepicker-9').val();
if(site != '' && startdate != '' && enddate != '' )
{
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url:"{{ route('billing.bill') }}",
type:"GET",
dataType:"json",
data:{
site:site,
startdate:startdate,
enddate:enddate,
_token:_token,
},
success:function(data){
},
error: function(data){
alert('Error');
}
})
} else
{
alert("Please select 'Site' & 'Date'. ");
}
});
Route :
Route::get('/home/billing','BillingController@Index');
Route::get('/home/billing/pdf','BillingController@bill')->name('billing.bill');
controller:
public function bill(Request $request)
{
//this query works when used for filter button but not work for convert button
$bills = DB::table('mydata')
->where('site', $request->site)
->whereBetween('report_date',[$request->startdate ,$request->enddate])
->get();
//dd($bills); get null
$pdf = \App::make('dompdf.wrapper');
$pdf =PDF::loadView('pdf',compact('bills'));
$pdf ->setPaper('a4','landscape');
return $pdf->stream('report.pdf');
}
Struggle with this for a long time but really don't know what's going wrong. Only Convert Button part cannot pass data to pdf view (data also not showing on pdf view)