如何在jquery

时间:2015-07-31 06:02:14

标签: php jquery session

我开发了一个购物车网站。如果国家变化,我想改变产品的价格。每当国家根据货币变化时,产品的价格应该在整个项目中。为此,我试图在jquery中设置一个会话变量,并且我想要检索数据。但是我无法得到它。

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

每个indidual页面的工作正常。这意味着每一页都会改变国家,然后改变产品的价格。为此,我想传递会话变量。但我无法在Jquery中设置会话变量。可以帮助我解决这个问题。 这里我插入了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'];
?>

2 个答案:

答案 0 :(得分:1)

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

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

您可以获得更多信息here

答案 1 :(得分:0)

你可以简单地在javascript var中获取任何php变量的值。 像

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