2014-02-19 69 views
0

我已经创建了一个我可以动态添加的表格&删除产品。得到关于选择产品的所有产品的价格总和

就像我已经添加了三个产品$ 10,$ 20,$ 30。

我暂时将这些产品添加到数据库中。

现在我想要没有刷新页面的所有产品的最终总和。

$(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); 
      } 
      }); 

    }); 

    }); 

我已经做了添加和删除,但我想快速求和每当我删除或添加的产品..

我希望你能得到它。

+0

你的'sum'函数或者你已经有'sum'值的地方? – Pavlo

+0

您可以在db中添加或删除数据后从db中计算总和并返回响应中的值。您可以在jquery success:函数中显示总和 –

回答

0

您可以发送ajax请求以获取购物车\临时数据库中产品的总和。

使用一些东西一样

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); 
      } 
      }); 
} 

,并呼吁get_product_sum时,你永远需要总和(增加产品的成功/删除产品sucess)

注:更合适的方式将来自DB时,你得到的总和永远有一个添加/删除,并返回它相同,然后分开它成功的添加/删除请求

相关问题