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

前端 未结 7 1078
轮回少年
轮回少年 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:10

    You cant access PHP session variables/values in JS, one is server side (PHP), the other client side (JS).

    What you can do is pass or return the SESSION value to your JS, by say, an AJAX call. In your JS, make a call to a PHP script which simply outputs for return to your JS the SESSION variable's value, then use your JS to handle this returned information.

    Alternatively store the value in a COOKIE, which can be accessed by either framework..though this may not be the best approach in your situation.

    OR you can generate some JS in your PHP which returns/sets the variable, i.e.:

    
        alert('".json_encode($_SESSION['msg'])."');
    ";
    ?>
    

提交回复
热议问题