简体   繁体   English

在 php 标签中包含 javascript 变量

[英]Include javascript variable inside php tags

i have javascript as below我有 javascript 如下

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

I want to add another javascript variable inside to __() function like this我想像这样在to __() function 中添加另一个 javascript 变量

<?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任何人都可以帮助我请问候

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): 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:然后 localize.php 应该如下所示:

<?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.说明:当您的客户端代码(Javascript)在浏览器中执行时,它会调用 URL,它将在服务器中执行您的服务器端代码(您的 PHP __(); 函数),然后将值返回给客户端代码.

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.但是,要使其正常工作,javascript 需要位于正在解释的.php 文件中。

The OP wants to include a JS variable in a PHP call, which is not possible, unless you use AJAX. OP 希望在 PHP 调用中包含一个 JS 变量,这是不可能的,除非您使用 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. js_call类似于使用sprintf或准备好的语句,但 js 除外。 The params are run through json_encode so the quoting and what not are correct.参数通过json_encode运行,因此引用和不正确的内容是正确的。 All these are stored in an array and then in the layout, just before my </body> i call:所有这些都存储在一个数组中,然后在布局中,就在我的</body>我调用之前:

<?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:然后它接受我使用js_call进行的所有调用,并在脚本标记内输出字符串值,结果如下:

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

Borrowed this brilliance from Apostrophe Cms借用了Apostrophe Cms的这种光彩

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:这将尝试将 PHP 变量“myvariable”放入脚本标签中,您想要的更接近:

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

However, in this case, why not just skip PHP completely?但是,在这种情况下,为什么不完全跳过 PHP 呢?

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

Javascript runs on client side and php on server side. Javascript 在客户端运行,php 在服务器端运行。 So var myvarible=200;所以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.那时myvariable将无效。

PHP is executed on the server, JS on the client. PHP在服务端执行,JS在客户端执行。 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.您不能指望 PHP 解析 JS,实际上 PHP 永远不会看到 JS 语句,因为它们只有在服务器处理完 PHP 后才会被处理。

To do localization in javascript (for whatever reason), echo __() can obviously not be called directly.要在 javascript 中进行本地化(无论出于何种原因), echo __() 显然不能直接调用。

There are different possible strategies有不同的可能策略

  • Include a localization string table in javascript when the page loads.当页面加载时,在 javascript 中包含一个本地化字符串表。 Do lookup against it when needed.在需要时进行查找。 This table could be generated on server-side using echo __() then cached.该表可以在服务器端使用 echo __() 生成,然后缓存。
  • Make ajax requests for server-localized data.对服务器本地化数据发出 ajax 请求。 Might not be suitable for frequent updates.可能不适合频繁更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM