[英]How to use PHP session variable in Javascript?
I have a .php file and .js file. 我有一个.php文件和.js文件。 I want to use the php
variable in javascript
file but its not working. 我想在javascript
文件中使用php
变量,但无法正常工作。 I found a solution by using session but that didn't worked.Can anybody provide me a solution or have a look at the code if there is any mistake. 我通过使用会话找到了解决方案,但是没有用。如果有任何错误,谁能提供给我解决方案或看看代码。
In .php 在.php中
$wd=500/$w;
$_SESSION['wd']=$wd;
In .js 在.js中
var sf= <?php echo json_encode($_SESSION['wd']);?>;
You can pass session value from php file to js by following method . 您可以通过以下方法将会话值从php文件传递给js。
php file :- PHP文件:-
$wd=500/$w;
$_SESSION['wd']=$wd;
echo '<script>var pf = '.json_encode($_SESSION['wd']).';</script>';
Js file :- js文件:-
var sf = pf;
You can simply pass the value through PHP like 您可以简单地通过PHP传递值,例如
$wd=500/$w;
$_SESSION['wd']=$wd;
echo '<script>var passedData = '.json_encode($_SESSION['wd']).';</script>';
And catch the the data in javascript like as 并像这样在javascript中捕获数据
var sf = passedData;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.