Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mariaaa's avatar

fail to pass user selected data to PDF view Laravel DOMPDF

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)

0 likes
8 replies
neilstee's avatar

@mariaaa the onclick="location.href = '/home/billing/pdf';" seems to be the problem, it's redirecting to that url before the ajax request happens.

What you can do is to add the location.href to the success method of your ajax request.

success:function(data){
	location.href = '/home/billing/pdf'; 
        // or window.location 
},
Mariaaa's avatar

@neilstee thanks for your reply. I have tried this and it gives me error 500 internal server error.

neilstee's avatar

@mariaaa can you post here the error? and what URL you landed on? check where you got redirected.

neilstee's avatar

well looks like you are submitting in the correct path? but your controller might be the problem?

Mariaaa's avatar

@neilstee this is how i write in my controller

    {        
      $bills = DB::table('mydata')
               ->where('site', $request->site)
               ->whereBetween('report_date',[$request->startdate ,$request->enddate])
              ->get();

          $pdf = \App::make('dompdf.wrapper');
          $pdf =PDF::loadView('pdf',compact('bills'));
          $pdf ->setPaper('a4','landscape');
       return $pdf->stream('report.pdf');
    }```


I'm not sure why my filter button able to filter this query and return data back to the first view. When it comes to convert button, dd( $request)  always return null.
Kyoung's avatar

Help I can't post here on the forum and have registered

Mariaaa's avatar

@kyoung go "My Question" and click on "New Discussion" then you can post your question

Please or to participate in this conversation.