Roshan Wijesena
Roshan Wijesena

Reputation: 3136

Include javascript variable inside php tags

i have javascript as below

 html="<th>"+<?php echo __(); ?>+"</th>";

I want to add another javascript variable inside to __() function like this

<?php echo __(<js varible>); ?>

I tried

var myvarible=200;
html="<th>"+<?php echo __("'"+myvarible+"'"); ?>+"</th>";
console.log(html);

not working for me

can any one help me please regards

Upvotes: 3

Views: 9742

Answers (6)

Fredrik
Fredrik

Reputation: 98

To do localization in javascript (for whatever reason), echo __() can obviously not be called directly.

There are different possible strategies

  • Include a localization string table in javascript when the page loads. Do lookup against it when needed. This table could be generated on server-side using echo __() then cached.
  • Make ajax requests for server-localized data. Might not be suitable for frequent updates.

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

var myvariable='<?php echo __("200"); ?>';
html="<th>"+myvariable+"</th>";
console.log(html);

However for this to work the javascript would need to be in a .php file that is being interpreted.

The OP wants to include a JS variable in a PHP call, which is not possible, unless you use AJAX. And you'll agree with me that code like this is only meant to cause big headaches and should be avoided at all costs.

Well yes and no.. i wouldnt do it this way. I use a helper that lets me do things like this in a consistent way. In my view file i have something like:

<?php js_call('jslib.myFunction(?,?)', __($value), 'some other value'); ?>

js_call its similar to using sprintf or a prepared statement except for js. The params are run through json_encode so the quoting and what not are correct. All these are stored in an array and then in the layout, just before my </body> i call:

<?php include_js_calls(); ?>

which then takes all the calls ive made with a js_call and outputs the string values inside a script tag resulting in something like:

<script type="text/javascript">
  jslib.myFunction('first value', 'some other value');
</script> 

Borrowed this brilliance from Apostrophe Cms

Upvotes: 1

Felipe Brahm
Felipe Brahm

Reputation: 3160

You have a misunderstanding on how server side and client side code work.

The only way that you could possibly achieve what you are trying to do (apply a PHP localization function to a Javascript variable) would be like this (this code assumes you are using JQuery but can be done without it too):

var myvariable = 'hello';
$.get('http://yoursite.com/localize.php?text='+myvariable, function(localizedText) {
  html = "<th>"+localizedText+"</th>";
  console.log(html);
});

And then localize.php should look like this:

<?php
include('you localization library');
echo __($_GET['text']);
?>

Explanation: while your client side code (Javascript) is been executed in the browser it will call a URL which will execute your server side code (your PHP __(); function) in the server and then return the value to the client side code.

Upvotes: 3

nico
nico

Reputation: 51640

PHP is executed on the server, JS on the client. You cannot expect PHP to parse JS, in fact PHP will never see the JS statements, because they will be processed only once the server has processed the PHP.

Upvotes: 1

Vivek Goel
Vivek Goel

Reputation: 24140

Javascript runs on client side and php on server side. So var myvarible=200; will be executed only on client side .

but will be get executed on server side. at that time myvariable will not be valid.

Upvotes: 1

Jim Deville
Jim Deville

Reputation: 10662

var myvarible=200;
html="<th>"+<?php echo __("'"+myvarible+"'"); ?>+"</th>";
console.log(html);

This would try to put the PHP variable "myvariable" into the script tag, what you want is closer to:

var myvarible=200;
html="<th>"+"<?php echo __("'myvarible'"); ?>"+"</th>";
console.log(html);

However, in this case, why not just skip PHP completely?

var myvarible=200;
html="<th>" + myvarible + "</th>";
console.log(html);

Upvotes: 1

Related Questions