2014-02-19 2 views
0

나는 동적으로 &을 추가 할 수있는 양식을 작성하여 제품을 삭제합니다.제품 선택시 모든 제품의 가격 합계를 받으십시오.

나는 $ 10, $ 20, $ 30의 세 제품을 추가했습니다.

i 임시로 제품을 데이터베이스에 추가하십시오.

지금은 페이지를 새로 고치지 않고 모든 제품의 최종 합계를 원합니다.

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

    }); 

    }); 

..

을 내가 추가 및 삭제했을하지만 난 삭제하거나 제품을 추가 할 때마다 빠른 합계를 원하는 난 u는 그것을 얻을 바랍니다.

+0

당신의 합계는 어디에서이고 또는 당신은 어딘가에 이미'sum' 가치가 있습니까? – Pavlo

+0

db에서 추가 또는 삭제 후 db의 합계를 계산하고 응답의 값을 반환 할 수 있습니다. jquery success : function에 합계를 표시 할 수 있습니다. –

답변

0

ajax 요청을 보내 cart \ temp DB에있는 제품의 합계를 얻을 수 있습니다.

은 (/ 제품 성공을 삭제 제품의 성공을 추가)

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 전화

참고 : 더 적절한 방법은 DB 때부터 합계를 얻을 수있을 것입니다 이제까지 추가/삭제가 있고 동일한 그것을 돌려 보내고 추가/삭제 요구의 성공에서 그 후에 그것을 분리하십시오

관련 문제