Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass PHP variable to JavaScript function from Blade format HTML

I am using Laravel Blade format for my HTML and I am passing multiple PHP variables to JavaScript functions. The issue is that the variables being passed to JavaScript function could have special characters.

I want the JavaScript function to be as follows:

jsFunction("argument1","argument2","argument3")

What I get:

jsFunction('argument1','argument2','argument3')

My Blade format code is:

<a href="#" onclick="displayBannerInvoice('{{$bannerProperty->property_id}}','{{ $bannerProperty->id }}','{{$bannerProperty->end_date}}','{{$bannerProperty->no_of_days}}','{{$bannerProperty->total}}','{{$bannerProperty->vat_percentage}}')"></a>
like image 265
Muhammad Raza Avatar asked Feb 11 '23 17:02

Muhammad Raza


2 Answers

The best shot is to not reinvent the wheel and try searching :)

There is a package already on github https://github.com/laracasts/PHP-Vars-To-Js-Transformer

It does exactly what you want. Its for Laravel 5 as well as Laravel 4.

like image 115
Kyslik Avatar answered Feb 13 '23 06:02

Kyslik


Here is an example of how I am doing it. I have a form with an update button which calls a JavaScript function.

{!! Form::button('Update', array('class' => 'btn btn-success', 'onClick' => 'updateTeamName(' . $team_id . ')')) !!}

I am passing the $team_id variable (passed to the view by the controller method) to the JavaScript function.

Hope this helps.

like image 32
KalC Avatar answered Feb 13 '23 08:02

KalC