简体   繁体   English

在选择产品时获得所有产品的价格总和

[英]get price sum of all product on selecting the product

i have created a form where i can dynamically add & delete the product. 我创建了一个表格,可以在其中动态添加和删除产品。

like i have added three product of $10, $20, $30. 就像我添加了$ 10,$ 20,$ 30三种产品。

i temporary add these product into the database. 我暂时将这些产品添加到数据库中。

now i want final sum of all product without refreshing page. 现在我想要所有产品的最终总和,而无需刷新页面。

 $(document).ready(function() {

    //##### send add record Ajax request to response.php #########
    $("#FormSubmit").click(function (e) {


            e.preventDefault();
            if($("#fund_amount").val()==='')
            {
                alert("Please enter amount");
                return false;
            }
            var fund_amount = $("#fund_amount").val();
            var fund_category=$("input[name=fundcategory]:checked").val();

            var dataString = 'fund_amount='+ fund_amount + '&fund_category=' + fund_category;

            jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:dataString,

            success:function(response){
                $("#productholder").append(response);
                $("#fund_amount").val(''); //empty text field on successful
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
            });
    });

    //##### Send delete Ajax request to response.php #########
    $("body").on("click", "#productholder .del_button", function(e) {
         e.returnValue = false;
         var clickedID = this.id.split('-'); //Split string (Split works as PHP explode)
         var DbNumberID = clickedID[1]; //and get number from array
         var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

            jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:myData, //Form variables
            success:function(response){
                //on success, hide  element user wants to delete.
                $('#item_'+DbNumberID).fadeOut("slow");
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
            });

    });

     });

i have done addition and deletion but i want quick sum whenever i delete or add the product.. 我已经完成添加和删除操作,但是无论何时删除或添加产品,我都想快速求和。

i hope u will get it. 我希望你能得到。

You can send a ajax request to get the sum of the products in the cart\\temp DB. 您可以发送ajax请求以获取cart \\ temp数据库中的乘积之和。

Use some thing like 使用类似

function get_product_sum()
{
     jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:"sum_of_the_product", //sum request methord
            success:function(sumasresponse){

               alert(sumasresponse)
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
            });
}

and call get_product_sum when ever you need sum (adding product success/ delete product sucess) 并在需要总和时调用get_product_sum (添加产品成功/删除产品成功)

NOTE : More proper way will be get the sum from DB when ever there is an addition/deletion and return it with the same and then separate it in success of addition/deletion request 注意:当有添加/删除操作时,更正确的方法是从数据库中获取总和,然后以相同的方式返回,然后在添加/删除请求成功后将其分开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM