简体   繁体   English

语法错误:在 javascript php 会话中

[英]SyntaxError: in javascript php session

I am using the following code in a seperate js file to use a php $_SESSION variable.我在单独的 js 文件中使用以下代码来使用 php $_SESSION 变量。 However, I am getting a syntax error of SyntaxError: missing ; before statement但是,我收到语法错误SyntaxError: missing ; before statement SyntaxError: missing ; before statement . SyntaxError: missing ; before statement I have tried putting the ;我试过把 ; in the usual place, but still the same.在通常的地方,但还是一样。

What is the correct way to use a php session in js file.在 js 文件中使用 php 会话的正确方法是什么。 Thanks谢谢

var companycode = '<?php echo $_SESSION['ls_idcode_usr']?>';

There may be special characters in the value of the session variable.会话变量的值中可能有特殊字符。 Use json_encode() to output a valid Javascript literal:使用json_encode()输出有效的 Javascript 文字:

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

You can only use PHP in files ending in .php , otherwise the web server won't parse them looking for code to execute, and it'll just get passed down to the client.您只能在以.php结尾的.php使用 PHP,否则 Web 服务器不会解析它们以查找要执行的代码,而只会将其传递给客户端。

You can certainly make a whatever.js.php file, where PHP will be involved in the production of the Javascript file that is passed down to the browser.您当然可以制作一个whatever.js.php文件,其中PHP 将参与生成传递给浏览器的Javascript 文件。

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

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