【问题标题】:How to set a session variable in jquery如何在 jquery 中设置会话变量
【发布时间】:2015-07-31 06:02:14
【问题描述】:

我正在开发一个购物车网站。如果国家/地区发生变化,我想更改产品的价格。产品的价格应该在整个项目中,每当国家/地区根据货币变化时。对于这个我试图在 jquery 中设置一个会话变量,我想用这个来检索数据。但我无法得到它。

如果假设我在索引页面中选择美元,它工作正常。如果我重定向到另一个页面。它不起作用。

每个单独的页面都可以正常工作。这意味着每个页面 iam 都会更改国家/地区,然后产品的价格也会发生变化。为此,我想传递会话变量。但是我无法在 Jquery 中设置会话变量。Anty one 可以帮我解决这个问题。 这里是我插入 Jquery 代码

Currency.format = 'money_with_currency_format';
var shopCurrency = 'INR';

//Sometimes merchants change their shop currency, let's tell our JavaScript file
Currency.money_with_currency_format[shopCurrency] = "${{amount}} INR";
Currency.money_format[shopCurrency] = "${{amount}} INR";

//Default currency
var defaultCurrency = 'INR' || shopCurrency;

//Cookie currency
var cookieCurrency = Currency.cookie.read();

//Fix for customer account pages
jQuery('span.money span.money').each(function () {
    jQuery(this).parents('span.money').removeClass('money');
});

//Saving the current price
jQuery('span.money').each(function () {
    jQuery(this).attr('data-currency-INR', jQuery(this).html());
});

//If there's no cookie.
if (cookieCurrency == null)
{
    if (shopCurrency !== defaultCurrency)
    {
        Currency.convertAll(shopCurrency, defaultCurrency);
    }
    else
    {
        Currency.currentCurrency = defaultCurrency;
    }
}
//If the cookie value does not correspond to any value in the currency dropdown.
elseif(jQuery('[name=currencies]').size() && jQuery('[name=currencies] option[value=' + cookieCurrency + ']').size() === 0)
{
    Currency.currentCurrency = shopCurrency;
    Currency.cookie.write(shopCurrency);
}
elseif(cookieCurrency === shopCurrency)
{
Currency.currentCurrency = shopCurrency;
}
else
{
    Currency.convertAll(shopCurrency, cookieCurrency);
}

//Currency value changes
jQuery('[name=currencies]').val(Currency.currentCurrency).change(function () {
    var newCurrency = jQuery(this).val();
    Currency.convertAll(Currency.currentCurrency, newCurrency);
    jQuery('.selected-currency').text(Currency.currentCurrency);
});

var original_selectCallback = window.selectCallback;
var selectCallback = function (variant, selector)
{
    original_selectCallback(variant, selector);
    Currency.convertAll(shopCurrency, jQuery('[name=currencies]').val());
    jQuery('.selected-currency').text(Currency.currentCurrency);
};

jQuery('.selected-currency').text(Currency.currentCurrency);
<?php
//PHP Session Created
$_SESSION['newCurrency'] = $_POST['currencies'];
?>

//Currencies Html Code
    < select id = "currencies" name = "currencies" >
                < option value = "INR" selected = "selected" > INR < /option> / / default
                < option value = "USD" > USD < /option>
                < option value = "CAD" > CAD < /option>
                < option value = "GBP" > GBP < /option>
                < option value = "EUR" > EUR < /option>
                < /select>
                //Display's generated session
<?php
echo $_SESSION['newCurrency'];
?>

【问题讨论】:

  • jQuery 无法访问您网站的服务器端,这就是为什么如果不请求后端脚本就无法设置会话变量
  • 那么我能做什么?检索会话变量?你能帮我吗。
  • 不能使用ajax设置和获取会话变量stackoverflow.com/questions/26343370/…
  • 那么我如何传递会话变量。

标签: php jquery session


【解决方案1】:

使用本地存储来存储该会话变量。

var sessionvalue = localStorage['sessionvalue'];
    if (!sessionvalue ) {
           //Do your stuff
    }

您可以获取更多信息here

【讨论】:

  • 你试过什么?你能设置一个小提琴吗?所以我可以查看它并尽可能帮助你
【解决方案2】:

您可以简单地在 javascript var 中获取任何 php 变量的值。 喜欢

<script>
    var sess = "<?php echo $_SESSION['Key']; ?>";
    ........
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多