将会话变量传递给 JavaScript
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
如果没有 echo,PHP 块不会输出任何内容,因此不会将任何内容插入到 Javascript 块中。您还缺少 JavaScript 中的尾随分号,这也可能导致致命的解析错误。
should be
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.
要设置该值,您需要回显/打印 $_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)