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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!