将PHP变量作为链接传递给视图上的定位标记时,出现未定义的变量错误

时间:2018-12-27 06:09:15

标签: php laravel anchor href

我正在使用Laravel应用程序,通过该应用程序我可以从API(这是一个可执行的链接,在单击该链接时会打开PDF文档)中获取一些数据。我正在获取存储在变量中的数据,然后传递到视图,但是当我将其添加到定位标记的href属性中时,出现错误 Undefined variable:quotePdf

但是当我在dd之后获取数据并将其粘贴到锚链接标记中时,它可以正常运行

AJAX代码呈现视图

 $.ajax({
    type: "POST",
    url: "getquote",
    data: JSON.stringify(allData),
    contentType: "application/json",
    dataType: "json",
    cache: false,
    success: function(data){ 
      //The url that will render the view called quote.blade.php
      window.location.href="showquote" ;
    },
    error: function(data) {
        var errors = '';
        for(datos in data.responseJSON){
            errors += data.responseJSON[datos] + '\n';
        }
        alert(errors);
    }
});

获取代码并显示在前端的控制器

 public function createQuote(Request $request)
    {

        $quote = $this->global_Curl_Meta(
            $data, 'api/travel/create-quote')->data;

        //dd($quote);

        $quote_data = $quote;

        $quoteholder_name = $form['FirstName'] . ' ' . $form['MiddleName'] . ' ' . $form['LastName'];
        $quoteholder_email = $form['email'];
        $travel_plan = $plan;
        $quoteID = $quote_data->QuotationId;
        $quoteRef = $quote_data->QuoteNumber;

        $data = [
            'sts_quote_number' => $quoteRef
        ];

        //STS PDF Quote
        $quotePdf = $this->global_Curl_Meta(
            $data, 'api/v1/travel/sts-quote-doc')->data;

        //dd($quotePdf);

        //Get all teh variables and return to the view
        $quote_data = view("B2C::travel.quote", compact('quote_data', 'quoteholder_name', 'quoteholder_email', 'travel_plan' ,'phone' , 'quotePdf' ));

        return 'true';
    }

dd之后的浏览器响应

data:application/pdf;base64,JVBERi0xLjQKJaqrrK0KNCAwIG9iago8PAovQ3JlYXRvciAoQXBhY2hlIEZPUCBWZXJzaW9uIFNWTiB0YWdzL2ZvcC0xXzEpCi9Qcm9kdWNlciAoQXBhY2hlIEZPUCBWZXJzaW9uIFNWTiB0YWdzL2ZvcC0xXzEpCi9DcmVhdGlvbkRhdGUgKEQ6MjAxODEyMjcwODA0NTkrMDInMDAnKQo+PgplbmRvYmoKNSAwIG9iago8PAogIC9OIDMKICAvTGVuZ3RoIDExIDAgUgogIC9GaWx0ZXIgL0ZsYXRlRGVjb2RlCj4+CnN0cmVhbQp4nJ2Wd1RU1xaHz713eqHNMNIZeu9tAOldQHoVhWFmgKEMMMwAIjZEVCCiiIhgAwkKGDAaisSKKBaCggL2gAQBJQaj2FDJjKyV+PLy3svL7497v7XP3ueeXc5aFwCSdyorLQuWAiCNJ+AHe7nSI6Oi6dh+AAM8wAAzAJisrAz/EM9QIJKPhxs9S+QE/kWvhwEkft8yEu8F/j9JszL4AgCgQBFbsDlZLBEXijg1R5Ahts+KmBqfImYYJWa+6IAilhNz4iIbfvZZZGcxs9N4bBGLz5zBTmOLuUfEW7OFHBEjfiIuyuZyckR8W8SaqcI0rojfimPTOExRDiiS2C7gsJJEbCpiEj802E3ESwHAkRK/4PgvWMDJFYiTckvPWM3nJiYJ6LosPbqZrS2D7s3JSeUIBEaBTFYKk8+mu6WnZTB5qwFYzPmzZMS1pYuKbG1ma21tZG5s9kWh/uvi35S4t4v0MuhzzyBa3x+2v/JLrwOAMSeqzY4/bPF7AejYDIDcvT9smocAkBT1rf3GF/nQxPOSJBBk2JmY5OTkGHM5LGNxQX/X/3T4G/rie8bi7X4vD92dk8AUpgro4rqx0lPThXx6VgaTxaEb/XmI/3HgX5/DMJiTwOFzeKKIcNGUcXmJonbz2FwBN51H5/L+UxP

通过AJAX渲染称为quote.blade.php的视图,除quotePdf以外的所有变量都渲染良好

@section('content')

@php
//var_dump($quote_data);
$quote_number = $quote_data->QuoteNumber;
$quote_details = $quote_data->Calculation_Quote->Calculation;
@endphp
<div class="container">

<main class="top Quote">
  <h1 class="quote-title">Travel Insurance Quote
    <!-- Go Back --> 
    <a href="#" onclick="goBack()">  
      <span class="back-us"><i class="fas fa-arrow-left"></i>Back</span>
    </a> 
    <!--END-->
  </h1>
  <table class="table ref">
  <thead class="thead-dark">
    <tr>
      <th colspan="6"> {{$quoteholder_name}} </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="4" class="highlighted">Description</td>
      <td colspan="2" class="highlighted">Pricing</td>
    </tr>
    <tr>
      <td colspan="4"> Quote Reference </td>
      <td colspan="2"> {{$quote_number}} </td>
    </tr>
    <tr>
      <td colspan="4">{{$travel_plan}} Package </td>
      <td colspan="2"> $ {{round($quote_details->TravelBasicPremium,2)}} </td>
    </tr>
    <tr>
      <td colspan="4"><form>
    </tr>

    <tr>
      <td colspan="4" class="highlighted">Total</td>
      <td colspan="2" class="highlighted">$ {{round($quote_details->TravelTotalGrossPremium,2)}}</td>
    </tr>

    <tr>
      <td colspan="2"><i class="fas fa-print quote"></i> <a href="{{ $quotePdf }}" target="_blank">Print Quote </a></td>
    </tr>

  </tbody>
</table>

<h1 class="notice">Important! Please review our policy, terms and conditions before you continue. <a href="#"><span class="noticeClick">CLICK HERE</span></a></h1>

<span class="glyphicon glyphicon-print"></span>

</main>
</div>

@endsection

4 个答案:

答案 0 :(得分:0)

尝试这个

$quote_data = view("travel.quote", ['quotePdf' => $quotePdf])->render();

否则使用with方法

$quote_data = view("travel.quote")->with('quotePdf',$quotePdf)->render();

答案 1 :(得分:0)

尝试这样

<a href="{!! $quotePdf !!}" target="_blank">Print Quote </a>

答案 2 :(得分:0)

我可能是错的,但这就是我所观察到的。

public function createQuote(Request $request)
            {

                $quote = $this->global_Curl_Meta(
                    $data, 'api/travel/create-quote')->data;

                //dd($quote);

                $quote_data = $quote;

                $quoteholder_name = $form['FirstName'] . ' ' . $form['MiddleName'] . ' ' . $form['LastName'];
                $quoteholder_email = $form['email'];
                $travel_plan = $plan;
                $quoteID = $quote_data->QuotationId;
                $quoteRef = $quote_data->QuoteNumber;

                $data = [
                    'sts_quote_number' => $quoteRef
                ];

                //STS PDF Quote
                $quotePdf = $this->global_Curl_Meta(
                    $data, 'api/v1/travel/sts-quote-doc')->data;

                //dd($quotePdf);

                //Get all teh variables and return to the view
                $quote_data = view("B2C::travel.quote", compact('quote_data', 'quoteholder_name', 'quoteholder_email', 'travel_plan' ,'phone' , 'quotePdf' ));

                return 'true';
            }

在您的紧凑函数中,您将“ phone”作为未在任何地方定义的条目进行传递,因此它将忽略此后的每个条目。 尝试从压缩功能中删除“电话”。

答案 3 :(得分:0)

而不是返回到base64字符串($ quotePdf)进行查看。您可以执行以下操作。

$decoded = base64_decode ($quotePdf);

$pdf = fopen ('quote.pdf','w');

fwrite ($pdf,$decoded);

fclose ($pdf);

header("Content-type:application/pdf");

header("Content-Disposition:attachment;filename='quote.pdf'");

readfile("quote.pdf");

此代码将解码base64字符串并创建pdf文件。并下载相同的。如果要返回视图,可以将pdf保存在服务器中,并作为响应返回文件路径。

希望这会有所帮助!