How to access PHP session variables from jQuery function in a .js file?

前端 未结 7 917
轮回少年
轮回少年 2020-11-28 10:50

How to access PHP session variables from jQuery function in a .js file? In this code, I want to get \"value\" from a session variable

$(function() {
   $(\"#         


        
相关标签:
7条回答
  • 2020-11-28 11:25

    I was struggling with the same problem and stumbled upon this page. Another solution I came up with would be this :

    In your html, echo the session variable (mine here is $_SESSION['origin']) to any element of your choosing : <p id="sessionOrigin"><?=$_SESSION['origin'];?></p>

    In your js, using jQuery you can access it like so : $("#sessionOrigin").text();

    EDIT: or even better, put it in a hidden input

    <input type="hidden" name="theOrigin" value="<?=$_SESSION['origin'];?>"></input>

    0 讨论(0)
提交回复
热议问题