简体   繁体   English

无法获取php会话变量以在javascript中设置var

[英]Cannot get php session variable to set a var in javascript

I have a session variable called $_SESSION['SESS_USER_TYPE'] in my php file which is set to a 2 character string. 我的php文件中有一个名为$_SESSION['SESS_USER_TYPE']的会话变量,该变量设置为2个字符串。 The php script redirects to an HTML file where I want to get the php session variable into a javascript variable. php脚本重定向到HTML文件,我要将php会话变量转换为javascript变量。

I am using it in my javascript as follows, but I get nothing in the javascript variable. 我在JavaScript中按如下方式使用它,但在javascript变量中却什么也没得到。

var user_type = <?php echo $_SESSION['SESS_USER_TYPE']; ?>; 

This does not work. 这是行不通的。

Further to input received from other users, I tried the following : 除了从其他用户收到的输入,我尝试了以下操作:

var user_type = '<?php echo json_encode($_SESSION['SESS_USER_TYPE']) ?>';

Javascript returns a syntax error to the above. Javascript返回上述语法错误。

I then tried 然后我尝试了

user_type = "<?php echo json_encode($_SESSION['SESS_USER_TYPE']) ?>";

This does not give an error in Javascript. 这不会在Javascript中产生错误。 However it returns the following value to user_type which is : 但是,它将以下值返回给user_type:

<?php echo json_encode($_SESSION['SESS_USER_TYPE']) ?>

$_SESSION['SESS_USER_TYPE'] is set to the string 'WO'.

Exactly what's the value in that session variable? 那个会话变量的值到底是什么? If it's a string, you're generating invalid Javascript and causing a syntax error, eg: 如果是字符串,则说明您生成的Javascript无效并导致语法错误,例如:

<?php

$x = 'hello';

?>
<script type="text/javascript">
var x = <?php echo $x; ?>;
</script>

will give you 会给你

var x = hello;

and probably blow things up. 可能会炸毁。

When dumping PHP data into a Javascript code block, especially when dynamically creating javascript values, ALWAYS use json_encode: 当将PHP数据转储到Javascript代码块中时,尤其是在动态创建JavaScript值时, 始终使用json_encode:

var x = <?php echo json_encode($x) ?>;

This will guarantee that you're producing syntactically valid JS. 这将确保您生成的语法有效的JS。

The file with that snippet of JavaScript will have to be parsed by PHP. 带有JavaScript片段的文件必须由PHP解析。 The simplest way to do this is use the .php extension. 最简单的方法是使用.php扩展名。 PHP variables and statements will not work in non-PHP files (unless you configure your webserver to do so but that's probably way out of scope for this question). PHP变量和语句在非PHP文件中不起作用(除非您配置Web服务器来这样做,但这可能超出此问题的范围)。 To use session vars, you also have to run session_start() . 要使用会话变量,还必须运行session_start()

For example 例如

some-file.php some-file.php

<?php session_start() ?>
<!DOCTYPE html>
<html>

<!-- snip -->

<script>
    var user_type = '<?php echo $_SESSION['SESS_USER_TYPE']; ?>';
</script>

As others have said, also include quotes around you JS strings. 正如其他人所说,还请在您的JS字符串周围加上引号。

The php script redirects to an HTML file where I want to get the php session variable into a javascript variable. php脚本重定向到HTML文件,我要将php会话变量转换为javascript变量。

How can you get value of session variable in HTML Page?? 如何在HTML页面中获取会话变量的值? You need PHP page for that. 您需要为此的PHP页面。 And in php page you can do like this 在php页面中,您可以这样

var user_type = "<?php echo $_SESSION['SESS_USER_TYPE']; ?>"; 

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

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