在 Laravel 中将输入数据从一个视图传递到另一个视图

Pass input data from one view to another view in Laravel

我有两个视图,第一个视图用于收集用户输入(用户 select 站点和日期)第二个视图是 pdf 视图(将显示在浏览器中)。现在的问题是,当用户 select 从第一个视图输入(站点和日期)并单击名为“转换按钮”的按钮时,控制器和第二个视图 (PDF) 无法获取数据。

试了很多次还是无法让过滤数据显示在pdf视图上

<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="BBSR">ABC</option>
                                <option value="BPSR">BBC</option>
                                <option value="BKSR">BBE</option>
                                <option value="FCSR">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>

按钮:

<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/weeklybilling/pdf';" > Convert to PDF</button> 
 </div>
 </div>

ajax:

  $('#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();
                var _token =$('input[name="_token"]').val();
            if(site != '' && startdate  != '' &&  enddate != '' )
            {  
                $.ajax({ 
                    paging: false,
                    searching: false,
                    processing: true,
                    retrieve: true,
                    serverSide: true,
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    url:"{{ route('weeklybilling.weeklypdf') }}",
                    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::get('/home/weeklybilling','WeeklyBillingController@Index');
Route::get('/home/weeklybilling/pdf','WeeklyBillingController@weeklybill')->name('weeklybilling.weeklybill'); 

控制器

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

         //dd($bills); get null
          $pdf = \App::make('dompdf.wrapper');
          $pdf =PDF::loadView('weeklypdf',compact('bills'));

       $pdf ->setPaper('a4','landscape');
       return $pdf->stream('weeklyreport.pdf');
        
    }

有谁知道如何知道如何从第一个视图获取输入数据并将其传递给第二个视图(pdf)? pdf视图中没有数据表**

是因为你的方法是GET,你发送的参数像POST方法,把路由改成POST或者像这样改变发送参数的方式:

             $.ajax({ 
                paging: false,
                searching: false,
                processing: true,
                retrieve: true,
                serverSide: true,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:"{{ route('weeklybilling.weeklypdf') }}" + "?site="+site+"&startdate="+startdate+"&enddate="+enddate+"&_token="+_token,
                type:"GET",
                success:function(data){
                   
                
                },
                error: function(data){
                    alert('Error');
                }
            }) 

希望有用。

如果你遇到和我一样的问题。这是你可以尝试的东西。

$.ajax({
    type: "POST",
    url: url,
    data: params,
    xhrFields: {
        responseType: 'blob' // to avoid binary data being mangled on charset conversion
    },
    success: function(blob, status, xhr) {
        // check for a filename
        var filename = "";
        var disposition = xhr.getResponseHeader('Content-Disposition');
        if (disposition && disposition.indexOf('attachment') !== -1) {
            var filenameRegex = /filename[^;=\n]*=((['"]).*?|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
        }

        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var URL = window.URL || window.webkitURL;
            var downloadUrl = URL.createObjectURL(blob);

            if (filename) {
                // use HTML5 a[download] attribute to specify filename
                var a = document.createElement("a");
                // safari doesn't support this yet
                if (typeof a.download === 'undefined') {
                    window.location.href = downloadUrl;
                } else {
                    a.href = downloadUrl;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.click();
                }
            } else {
                window.location.href = downloadUrl;
            }

            setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
        }
    }
});

它帮助我将数据传递到 pdf 视图,但我现在的问题是它无法下载。