我无法在 jquery 中读取用 PHP 定义的会话


i am not able read session which is defined in php in jquery

我现在正在 php 中分配会话,我正在尝试在脚本标签中读取 jquery 中的会话值,请向我建议一些方法来读取 jquery 中的会话值,而无需使用 phptags ()。

 <?php 
           if(!isset($_SESSION)){
               session_start();
           }
           $_SESSION['name'] = "testname";
           echo $_SESSION['name']; 
        ?> 
    //with this i will get name in session but how to read in 
        <script>
            $(document).ready(function(){
                var name =  ???? //
                console.log(name);
            });
        </script>
how to r`enter code here`ead here with out php tags in script

绝对不是解决此问题的方法,但这里有一个快速的技巧来解决您的问题:

<?php
   if(!isset($_SESSION)){
       session_start();
   }
   $_SESSION['name'] = "testname";
   echo $_SESSION['name']; 
?>
<script>
    $(document).ready(function(){
        var name =  <?php echo $_SESSION['name']; ?>;
        console.log("Name session variable is: " + name);
    });
</script>