Using PHP variables in jQuery plugin initialization? - javascript

I have a jQuery image gallery plugin that I'm using extensively throughout a site for which I'm building PHP templates. In the properties for the plugin initialization, most of the values will remain the same throughout the site, aside from a few exceptions. In these instances, I'm wondering if it's ok for me to pass in a value for the property via PHP; e.g. if there is a property called "thumbnailsVisible" in certain cases I will want this set to true, and in others, false. Thus, is there anything wrong with assigning a value to a corresponding variable and including the following in the jQuery initialization within the template:
thumbnailsVisible : <?php echo $thumbnailsVisible; ?>
I realize I could also just externalize the initialization in a js file and create different versions as needed, but this would be much simpler provided there isn't anything wrong with this approach...
Also, if there's a completely different approach that would be optimal, I'd appreciate any sort of assistance. Thanks.

Yes. You can do this.
PHP is processed on the server side and only sent to client when it's processed.
You could write a php script to return the JavaScript and pass in an argument.
Eg.
<?php
$thumbnailVisible = isset($_GET['showthumb']) ? $_GET['showthumb'] : 'false';
?>
$.func({
thumbnailsVisible: <?php echo $thumbnailVisible; ?>
});
If it doesn't work correctly, try specify the content-type using header().
header('Content-type: application/javascript');
Place this at the top of the PHP script.

Yes, you could do that , actually i come across cases like that very often and i usually do the same .
You can check the code here :
(Although that is based on codeIgniter)

Related

php hide variable from javascript [duplicate]

I created now a Javascript Code that get the php variable into javascript code, my issue that the php variable is important and I don't want any can see this variable is there is any way to do that by the way I tried to use obfuscator but it doesn't work because of the PHP code inside the Javascript code, let's say this is my Code,
<?php
$var = "this is impotant";
?>
<script type="text/javascript">
var javaScriptVar = "<?php echo $var; ?>";
</script>
So, is there any way to use PHP variables in Javascript code or hide the result of the PHP code?
Nobody sees the PHP code. But if you expose values into Javascript, they are not secret anymore. There is no way to deal with this. You cannot use the value in Javascript and NOT reveal it.
If you want to keep process data secret on the server, and available for the next request of that user, use a session.
People will only see the value of the variable. They wont know what it is or how important it is supposed to be. Nobody will see the variable name because the PHP code is executed BEFORE the page is sent to the client. Therefore there is no need to obfuscate the value, and you cant anyway since you need the value.
An example. if I use this PHP code in my file
<p>Hello Mr <?php echo $MY_SUPER_SECRET_VARIABLE ?></p>
the only thing people will be able to see in the source when the page loads is
<p>Hello Mr Bond</p>
The same rule applies if it is placed in Javascript
First you need to understand that Javascript is executed on the client side, every piece of code and variable are in some way accessible by someone with some programming background.
Although you can obfuscate the source code and encrypt the variable to make it harder to read, there is no 100% protection when things happen on client side.
who wants to get the value, will get it. but you can
dynamically inject them via ajax
encode (base64 etc.) the value
obfuscate the code
PHP files will be interpreted into static (like html or xml format) file, means that all variables will be replaced with certain values.What users see is static, no php code displayed but just interpreted text.

how to get the whole laravel language(localization) data onto view?

I want to use the laravel language data at the frontend part in the browser.
So I can use sprintf in js to show the same translation in frontend part.
I wish it could be an big javascript object. It would be very easy and nice to use.
Thanks in advance.
This is a situation I've dealt with myself and the answer is a bit awkward.
You can use Lang::get('file') to output the entire file as an array, which you can use with json_encode to do exactly what you want. But, as far as I know, you cannot get all language files. This probably has to do with the fact that Laravel cannot scan the directory for all file names without consuming a lot of disk resources, and does not do so normally. Language is handled with lazy loading in all applicable cases.
But! What you can do, is put all your JavaScript-related language in one file. I called mine widgets.php, and then do json_encode( Lang::get('widget') ) to get your JavaScript relevant language in one object.
Or, if you really do need all of it, you can build it manually.
$lang = ['auth', 'validation', 'etc'];
for ($files as $file)
{
$lang[$file] = Lang::get($file);
}
return json_encode($lang);
I guess easiest way to do that is to use cookies. Set coockie with Laravel:
$cookie = Cookie::make($name, $value);
Then read and parse it with JS:
var x = document.cookie;

How to have an action respond to javascript in PHP?

I'm currently working with a Kohana project and am trying to implement endless scrolling. I'm trying to use the method Ryan Bates shows in the following Ruby on Rails video:
https://youtu.be/PQX2fgB6y10?t=2m29s
At 3:21 he says the action won't respond to javascript and proceeds to create a js.erb file. What is the PHP equivalent to this step going forward? Where would I place the php file equivalent to his index.js.erb?
To copy what he did, just create a PHP file that generates Javascript with the content you want to append like he did with render(). You can call this file whatever you want, but following his convention, it would be index.js.php.
So for example:
$('#products').append('<?php render_elements(); ?>');
$('#pagination').replaceWith('<?php render_pagination(); ?>');
Since you're already this far in his tutorial, I assume that you have the code to render the elements you want to display already.
It may be easier however to use other AJAX methods to achieve the same thing though.

Is it possible to use $_SESSION with a Javascript file?

I have a Javascript file main.js and I need to use echo $_SESSION["username"] within it, but it doesn't work obviously. Is there any way I can do this / some kind of workaround?
You cannot execute PHP code in a .js file but there are ways to work around it. You could use an ajax call to get that variable (check out this link for ajax tips).
Depending on the information you wish to store, you could also get PHP to echo it into a specific element in your html page and then use JS to look up that element like this:
echo "<span id='usernameForJS' style='display: none;'>".$_SESSION['USERNAME']."</span>";
And then go and grab that elements inner html with JS like this:
var username = document.getElementById('usernameForJS').innerHTML;
I would advise AJAX just because then there is no need for almost redundant elements in your page. And obviously don't use the second method for anything sensitive.
There is a option I saw on css-tricks.com
https://css-tricks.com/snippets/htaccess/use-php-inside-javascript/
I have never tried on my own but commentators said it works.
There is also a explanation of how you could do this in the comments:
https://css-tricks.com/snippets/htaccess/use-php-inside-javascript/#comment-85757
But don't forget taht this is a rather "hacky" way to do it, you should consider to open that javascript file and instantiate your class/function(or whatever is in this file) from the .php-page.

Where is the best place to put the custom jquery and javascript in Zend Framework?

where is the best place to put the jquery and javascript scripts in zend framework?
shall i put every script in js file and append in the controller like :
$this->view->headScript()->appendScript
or to leave in the phtml page as is ?
I'd say it depends on your scripts, if your script is linked to your controller, you could include it in the _init() method of your controller.
I usually prefer to include my .js scripts inside my view (when they concern only one unique view) using $this->jQuery()->addOnload() for jQuery and:
<?php $this->headScript()->appendFile('/js/user-list.js') ?>
<?php $this->headScript()->captureStart() ?>
site = {
baseUrl: "<?php echo $this->baseUrl() ?>"
};
<?php $this->headScript()->captureEnd() ?>
for js scripts.
As you can see in this second example, a real advantage to use captureStart() is that you can use PHP to generate contents in your Javascript. It can be pretty useful for translating a word for example.
Finally, _initView() method in your bootstrap is a good place to put web-site relative .js.
Javascript you need for the entire site you want to add during bootstrap, in a view setup or initView method.
Javascript you need for some controller, you should append in the init() method of that controller.
Javascript you only need for a certain action, you should append in that action.
This way the client never has to download more data/files than really needed.

Categories

Resources