[英]Getting undefined variable error when passing a PHP variable as a link on an anchor tag on the view
Am working on a Laravel application whereby I fetch some data from an API (which is an executable link which opens a PDF document when clicked). 我正在使用Laravel应用程序,因此我可以从API(这是一个可执行的链接,在单击该链接时会打开PDF文档)中获取一些数据。 Am fetching the data storing in a variable then pass to the view but when I add it inside the href attribute of anchor tag I get error of Undefined variable: quotePdf . 我正在获取存储在变量中的数据,然后传递到视图,但是当我将其添加到定位标记的href属性内时,出现未定义变量的错误:quotePdf 。
But when I get the data after dd and paste it in the anchor link tag it works perfectly 但是,当我在dd之后获取数据并将其粘贴到锚链接标记中时,它可以完美运行
AJAX code rendering the view 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);
}
});
Controller that fetches the code and shows on the frontend 提取代码并显示在前端的控制器
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';
}
Response on the browser after dd 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
The view that is called quote.blade.php being rendered via AJAX, all the variables are being rendered well except quotePdf 通过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
try this one 试试这个
$quote_data = view("travel.quote", ['quotePdf' => $quotePdf])->render();
otherwise used with
method 否则with
方法一起使用
$quote_data = view("travel.quote")->with('quotePdf',$quotePdf)->render();
这样尝试
<a href="{!! $quotePdf !!}" target="_blank">Print Quote </a>
I may be wrong but this is what I have observed. 我可能是错的,但这就是我所观察到的。
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';
}
In your compact function, you are passing 'phone' as an entry which is not defined anywhere and hence it is ignoring each and every entry after that. 在您的紧凑函数中,您将“ phone”作为未在任何地方定义的条目进行传递,因此它将忽略此后的每个条目。 Try to remove 'phone' from compact function. 尝试从压缩功能中删除“电话”。
Instead of returning to base64 string ($quotePdf) to view. 而不是返回base64字符串($ quotePdf)进行查看。 You can do something like this. 你可以做这样的事情。
$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");
This code will decode base64 string and create a pdf file. 此代码将解码base64字符串并创建pdf文件。 and downloads the same. 并下载相同的内容。 If you want return the view you can save the pdf in server and return the file path as a response. 如果要返回视图,可以将pdf保存在服务器中,并作为响应返回文件路径。
Hope this helps!! 希望这可以帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.