最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I pass variables from JavaScript to PHP? - Stack Overflow

programmeradmin2浏览0评论

I am using jQuery to calculate prices. Here is a small snippet of my JavaScript:

// Prices: radio
var curLam = "gloss";
$("input[name=lamination]").click(function () {
    var gloss = 5;
    var matt = 6;
    if ($(this).val() == 'gloss' && curLam != 'gloss') {
        $('#prices span').text(parseInt($('#prices span').text()) + gloss - matt);
        curLam = 'gloss';
        console.log('1');
    }
    if ($(this).val() == 'matt' && curLam != 'matt') {
        $('#prices span').text(parseInt($('#prices span').text()) - gloss + matt);
        curLam = 'matt';
        console.log('2');
    }
    $("#prices span").each(function () {
        var $priceValue = $(this);
    })
});

This checks to see if a matte or gloss finish has been selected and then it inserts the price into the span within the prices div tag.

I need to know how to assign that price value into a variable which can then be passed on to PHP for my shopping cart.

I am using jQuery to calculate prices. Here is a small snippet of my JavaScript:

// Prices: radio
var curLam = "gloss";
$("input[name=lamination]").click(function () {
    var gloss = 5;
    var matt = 6;
    if ($(this).val() == 'gloss' && curLam != 'gloss') {
        $('#prices span').text(parseInt($('#prices span').text()) + gloss - matt);
        curLam = 'gloss';
        console.log('1');
    }
    if ($(this).val() == 'matt' && curLam != 'matt') {
        $('#prices span').text(parseInt($('#prices span').text()) - gloss + matt);
        curLam = 'matt';
        console.log('2');
    }
    $("#prices span").each(function () {
        var $priceValue = $(this);
    })
});

This checks to see if a matte or gloss finish has been selected and then it inserts the price into the span within the prices div tag.

I need to know how to assign that price value into a variable which can then be passed on to PHP for my shopping cart.

Share Improve this question edited Jan 28, 2020 at 9:11 ankitkanojia 3,1224 gold badges24 silver badges37 bronze badges asked Jun 11, 2009 at 12:19 user88039user88039 311 gold badge1 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

Generally speaking, you shouldn't return the selected price to the server. HTML forms can be easily faked. It is far safer to send the user's choice back to the server, which also knows how much it should cost.

Basically Javascript validation (which this essentially is) is convenient but shouldn't be trusted. You already have an input for selecting the finish. Just send that back to the server. That way your site will still work (or work better) if the user has Javascript disabled.

You could put the ID of the product into an input element and then when you submit into the shopping cart, then the value should be in your php $_POST variables.

There you can check the price and other information straight from your database.

发布评论

评论列表(0)

  1. 暂无评论