Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

How can I pass variables from JavaScript to PHP?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...