PHP Variables to JavaScript Variables in CodeIgniter

Often, you’ll find yourself in situations, where you want to pass some server-side string/array/whatever to your JavaScript. Traditionally, this can be a bit of a pain – especially as your app grows.

Transform PHP Vars to JavaScript” is a codeIgniter library and it simplifies the process drastically. And it is developed based on “PHP-Vars-To-Js-Transformer” it is developed by JeffreyWay.

PHP Variables to JavaScript Variables in CodeIgniter

How to install

Download the files add put PHPtoJS.php class in your library folder application\libraries\PHPtoJs.php. Now load the library locally or globally.

Locally

Globally

Open you autoload.php file which located at application\config\autoload.php

After loading library, you may use in your controllers as shown below.

Final step – in your layout page or view page add this line, which will print the JavaScript variables.

That is it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status