将 PHP 变量传递给 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5310216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:35:19  来源:igfitidea点击:

Passing PHP variable into JavaScript

phpjavascript

提问by ptamzz

I've a PHP session variable, $_SESSION['user'], alive throughout the session. In the head section, I've my JavaScript file included, scripts.js.

我有一个 PHP 会话变量 ,$_SESSION['user']在整个会话期间都处于活动状态。在 head 部分,我包含了我的 JavaScript 文件scripts.js.

How do I pass the session variable into the JavaScript file if I want something like the following.

如果我想要类似以下内容,如何将会话变量传递到 JavaScript 文件中。

$.("#btn').click (
     function() {
        alert('<?php echo $_SESSION['user']; ?>');
     }
)

As the <?php ?>isn't recognized in the JavaScript file, the code above doesn't work. So I've to put in the PHP file itself, but how do I keep it in the JavaScript file?

由于<?php ?>JavaScript 文件中无法识别 ,因此上面的代码不起作用。所以我必须放入 PHP 文件本身,但如何将它保存在 JavaScript 文件中?

回答by Chris

In your PHP file you could set your user as a global varibale:

在您的 PHP 文件中,您可以将您的用户设置为全局变量:

<script type="text/javascript">
    var ptamzzNamespace = {
       sessionUser : '<?php echo $_SESSION['user']; ?>'
    }        
</script>

Include this before including your external JavaScript code.

在包含外部 JavaScript 代码之前包含它。

Then in your JavaScript file you can use it:

然后在你的 JavaScript 文件中你可以使用它:

$.("#btn').click (
     function() {
        alert(ptamzzNamespace.sessionUser);
     }
)

Additionally:If you're not sure if your session varibale can contain quotes itsself, you can use addslashes()to fix this problem:

另外:如果你不确定你的会话变量是否可以包含引号本身,你可以addslashes()用来解决这个问题:

<?php echo addslashes($_SESSION['user']); ?>even if this will maybe produce something you don't really want to display (because it produces a string with slashes) it will help that your code will not fail. (Thanks to Artefacto)

<?php echo addslashes($_SESSION['user']); ?>即使这可能会产生您并不真正想要显示的内容(因为它会产生一个带斜杠的字符串),它也会帮助您的代码不会失败。(感谢 Artefacto)

Would this be an option for you?

这会是你的选择吗?

回答by sushil bharwani

Set the userID (in PHP) in an input type hidden in your file. In your JavaScript code you can read the hidden input types value.

在文件中隐藏的输入类型中设置用户 ID(在 PHP 中)。在您的 JavaScript 代码中,您可以读取隐藏的输入类型值。

回答by Dan Herd

You could set your server up to parse .js files as PHP. For example, in Apache config or .htaccess file:

您可以将服务器设置为将 .js 文件解析为 PHP。例如,在 Apache 配置或 .htaccess 文件中:

<FilesMatch "scripts.js">
    AddHandler application/x-httpd-php5 .js
</FilesMatch>

回答by diEcho

try with below logic

尝试以下逻辑

print ' <script type="text/javascript" language="javascript"> 
function checkForm() 
{ 
     var username = "'.$_SESSION['user'].'"; 
     if( username == '') 
     { 
        alert('No Times selected'); 
        return false; 
      }
}
</script>';

回答by Andre Backlund

Parse the included JavaScript file as PHP

将包含的 JavaScript 文件解析为 PHP

Create a .htaccess file in you JavaScript directory and add the following line.

在您的 JavaScript 目录中创建一个 .htaccess 文件并添加以下行。

AddHandler php-cgi .js

<?php ?>tags till now be recognized by your JavaScript file.

<?php ?>到目前为止,您的 JavaScript 文件可以识别标签。

Use Ajax to fetch the user ID

使用 Ajax 获取用户 ID

Create a file called getID.php which only echoes the users ID. Then in your JavaScript file you can use the following code.

创建一个名为 getID.php 的文件,它只回显用户 ID。然后在您的 JavaScript 文件中,您可以使用以下代码。

$.get('getID.php', function(uid) {
  alert('User ID is:' + uid);
});

Create a script tag before including the JavaScript printing user ID.

在包含 JavaScript 打印用户 ID 之前创建脚本标记。

<script type="text/javascript">
userID = <?php echo $_SESSION['user']; ?>;
</script>

You will now have access to the users ID using variable userID.

您现在可以使用变量 userID 访问用户 ID。