drhe80011 2013-09-11 03:53
浏览 118
已采纳

将jQuery变量传递给PHP

I have an order form I'm working on, where I'm using jQuery to update the price in real time when the user selects different options. So, right now, the final project cost, package type, etc are set in jQuery variables, which I need to convert to PHP to insert into the database.

You can easily see the code: http://jsfiddle.net/cadengrant/uddPA/2/

And live preview of working code: http://www.htmlified.com/order-now/

function update_price() {
        var base_cost = base_price;
        var basic_price = base_price;
        var pro_price = base_price;

        jQuery('.packages .selected').each(function(index) {
            base_cost += jQuery(this).data("price");
            basic_price += base_cost;
            pro_price += base_cost + 70;
        });

        jQuery('#markup-pages').each(function(index) {
            var price = Number(jQuery(this).val());
            var packages = jQuery('.packages .selected').data("price");
            var pages = 0;

            jQuery('#packages .selected').each(function(index) {
                if(jQuery(this).hasClass('basic')) {
                    if(packages == 199) {
                        pages = price * 99 - 99;
                    } else if (packages == 189) {
                        pages = price * 94 - 94;
                    } else if (packages == 399) {
                        pages = price * 199 - 199;
                    }
                } else if (jQuery(this).hasClass('pro')) {
                    if(pro_price == 269) {
                        pages = price * 134 - 134;
                    } else if (pro_price == 259) {
                        pages = price * 129 - 129;
                    } else if (pro_price == 469) {
                        pages = price * 234 - 234;
                    }
                }
            });

            base_cost += pages;

            /* Single or plural page text */
            if(price == 1) {
                var markup_pages = "markup page";
            } else {
                var markup_pages = "markup pages";
            }
            jQuery('.markup-pages').text(markup_pages);
        });

        jQuery('#packages .selected').each(function(index) {
            if(jQuery(this).hasClass('pro')) {
                base_cost += 70;
            }
        });

        /* Update Doctype */
        jQuery('input[name=page_doctype]:checked', '#order-form').each(function(index) {
            var basic_doctype_text = "";
            var pro_doctype_text = "";
            if(jQuery(this).hasClass('doctypehtml')) {
                var doctype_text = "W3C Valid HTML5 & CSS3";
            } else if (jQuery(this).hasClass('doctypexhtml')) {
                var doctype_text = "W3C Valid XHTML1 & CSS2";
                basic_doctype_text += " Transitional";
                pro_doctype_text += " Strict";
            }

            jQuery('.doctype-text').html(doctype_text);
            jQuery('.basic-doctype').html(doctype_text + basic_doctype_text);
            jQuery('.pro-doctype').html(doctype_text + pro_doctype_text);
        });

        jQuery('.price').html(base_cost);
        jQuery('.basic-price').html(basic_price);
        jQuery('.pro-price').html(pro_price);
    }

I just need to figure out how to pass those variables (doctype text, basic doctype, pro doctype, base_cost, etc etc) in the JS section to my order.php form, so I can update amount paid, the package type they selected, etc. Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongliang7545 2013-09-11 04:12
    关注

    You already have a form in your page. I suggest you create hidden inputs in this form to be submitted with the form. Ex :

    <input type="hidden" name="base_cost" value="999">
    

    and you can adjust the value easily with jquery. After submitting the form to the php page you can capture these values using :

    $base_cost = $_POST['base_cost'];
    

    But don't forget to sanitize and validate every input from the user. Hope this helps and excuse my English.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?