开发者

PHP variable to JS - over and over

Sorry my Gods, but....

I have php, file, and in this php i set the language, 开发者_JAVA百科and include the necessary lang file:

 ...
     if ($lan=='ge') {$_SESSION['lang']='german';...} 
     if ($lan=='en') {$_SESSION['lang']='english'; ....} 
  } 
  if ($_SESSION['lang']=='english'){
    include ..english
  }else{
    include ...german

  }

I use Yshout in my site, and want to make it multilang. In the JS file the developer use some text which i have to make it multilanguage.

So in JS file the first line:

`<?php header('Content-type: text/javascript');?>

And in the php:

 <script src="/js/yshout.php" type="text/javascript"></script>

Now i can use PHP in JS file. So i try to use instead of the fix text. But no results, 'coz JS don know $text variable. If i include the language file in the JS then OK, but i have to include the necessery language file, this is not work, 'coz JS dont know the $_SESSION['lang'] varible when he run.

Which is the simplest way to tell JS which language file have to be included and how can i do that?

Thank you


When the client asks for /js/yshout.php a (completely) new instance of php is created on the server to handle this new request. This new instance does not share the variables of the "old" instance that served the request for the html document (including the <script ...src="yshout.php"> element).
Therefore if you want to access the session data you have to call session_start() again and (as you've already noticed) assign values to all the variables you want to use.


Rather than using Js to include your files, you need to use PHP and modify your script. And because you have put following line in Js file, but the code is that of PHP, it won't execute:

 <?php header('Content-type: text/javascript');?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜