将会话变量传递给 JavaScript

发布于 2024-12-03 16:02:44 字数 2044 浏览 1 评论 0原文

在 ExpressionEngine 模板中,我在 javascript 文件中设置 PHP 会话变量,如下所示:(是的,EE 将解析 PHP 并将插入值添加到 javascript)

<?php session_start(); ?>  
function check_someone_else_result(data) {
  waiting_list_flag = false;
   <?php $_SESSION['waiting_list_flag'] = false;  ?>
  if(data.CanBuy=='YES'){
    show_for_someone_else();
  } else {
    waiting_list_flag = true;
   <?php $_SESSION['waiting_list_flag'] = true;  ?>
    $('#sm_content .oblcontent').html($('#waiting_list').html());
    $('#sm_content a.closebtn').click(function(){location.reload(true);});
    $('#sm_content a.yeswaitbtn').click(function(){show_for_someone_else();});
  } // if(data.CanBuy=='YES')
} // function check_someone_else_result

现在,在 show_for_someone_else() 函数中,我正在重定向到另一个加载另一个 javascript 文件的页面,我试图将 javascript 变量设置为与上面设置会话变量相同的值。

<?php session_start(); ?>
   var CART_URL          = '{site_url}store/checkout/cart/';
   $(document).ready(function(){
     // attach the validationEngine to the form
     $("#voucher_form").validationEngine('attach', {
                      scroll: false
     }); // $("#voucher_form").validationEngine

     // handles the NExt button click
     $('#checkout-step-billing a').click(function(){
      $('#checkout-step-billing .voucher_form').submit();
     }); // $('#checkout-step-billing a').click

     // handles keypresses in all the fields in the form to submit the 
     // form when the press enter
     $("#checkout-step-billing .voucher_form input").keypress(function (e) {
      if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        $('#checkout-step-billing .voucher_form').submit();
        return false;
      } else {
        return true;
      }
     }); // $("#checkout-step-billing .voucher_form input").keypress

     // set the wait_list_flag from the session variable
     var wait_list_flag = <?php $_SESSION['waiting_list_flag']; ?>   
   }); // $(document).ready

但我什么也没得到。

我需要怎样做?

In an ExpressionEngine template, I'm setting a PHP session variable in a javascript file like this: (Yes, EE will parse the PHP and add plug the values to the javascript)

<?php session_start(); ?>  
function check_someone_else_result(data) {
  waiting_list_flag = false;
   <?php $_SESSION['waiting_list_flag'] = false;  ?>
  if(data.CanBuy=='YES'){
    show_for_someone_else();
  } else {
    waiting_list_flag = true;
   <?php $_SESSION['waiting_list_flag'] = true;  ?>
    $('#sm_content .oblcontent').html($('#waiting_list').html());
    $('#sm_content a.closebtn').click(function(){location.reload(true);});
    $('#sm_content a.yeswaitbtn').click(function(){show_for_someone_else();});
  } // if(data.CanBuy=='YES')
} // function check_someone_else_result

Now, in the show_for_someone_else() function, I'm redirecting to another page that loads another javascript file and I'm trying to set a javascript variable to the same value that I set the session variable to above.

<?php session_start(); ?>
   var CART_URL          = '{site_url}store/checkout/cart/';
   $(document).ready(function(){
     // attach the validationEngine to the form
     $("#voucher_form").validationEngine('attach', {
                      scroll: false
     }); // $("#voucher_form").validationEngine

     // handles the NExt button click
     $('#checkout-step-billing a').click(function(){
      $('#checkout-step-billing .voucher_form').submit();
     }); // $('#checkout-step-billing a').click

     // handles keypresses in all the fields in the form to submit the 
     // form when the press enter
     $("#checkout-step-billing .voucher_form input").keypress(function (e) {
      if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        $('#checkout-step-billing .voucher_form').submit();
        return false;
      } else {
        return true;
      }
     }); // $("#checkout-step-billing .voucher_form input").keypress

     // set the wait_list_flag from the session variable
     var wait_list_flag = <?php $_SESSION['waiting_list_flag']; ?>   
   }); // $(document).ready

But I am not getting anything at all.

How do I need to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城旧梦 2024-12-10 16:02:44
 var wait_list_flag = <?php $_SESSION['waiting_list_flag']; ?>   

应该是

 var wait_list_flag = <?php echo $_SESSION['waiting_list_flag']; ?>;
                            ^^^^                                   ^

如果没有 echo,PHP 块不会输出任何内容,因此不会将任何内容插入到 Javascript 块中。您还缺少 JavaScript 中的尾随分号,这也可能导致致命的解析错误。

 var wait_list_flag = <?php $_SESSION['waiting_list_flag']; ?>   

should be

 var wait_list_flag = <?php echo $_SESSION['waiting_list_flag']; ?>;
                            ^^^^                                   ^

Without the echo, the PHP block doesn't output anything, so nothing gets inserted into the Javascript block. You're also missing the trailing semicolon in the javascript, which may also cause a fatal parse error.

烟雨凡馨 2024-12-10 16:02:44

要设置该值,您需要回显/打印 $_SESSION['waiting_list_flag'];在第二个片段中。

请记住,在第一个代码片段中,PHP 将首先运行(它不与 JavaScript 一起运行),因此会话变量将始终为 true。 (除非这就是你对 EE 的意思)

To set the value, you will need to echo / print the $_SESSION['waiting_list_flag']; in the second snippet.

Keep in mind, in the first snippet, PHP will be run first (it's not run with the JavaScript), so the session var will always be true. (Unless that is what you meant wrt EE)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文