Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i calculate the total amount of Multiple products in Javascript? Values are coming in AJAX

Tags:

php

$.ajax({
    type: "POST",
    url: 'ajax_subtotal.php',
    data: listing_id=listId,
    dataType:'json',
    success: function(data)
    {
        if(data['result']=='success')
        {
            alert(data['pricing']);
        }
}

data['pricing'] gives me the price of one product..The function is calling if product quantity is changed. So, how can I calculate the total price of the multiple products at the same time??

like image 723
aliasgar vanak Avatar asked Apr 12 '13 05:04

aliasgar vanak


2 Answers

First you have declare 
var total = 0
$.ajax({
 ...
 success:function(){
     if(data['result']=='success')
        {
           total += data['pricing'];
        }
   }
 });

atlast of the function alert(total);
like image 23
Gangnam Avatar answered Sep 25 '22 20:09

Gangnam


First i store all the value in to the hidden field..

<input type="hidden" name="NumberOfProperty[]" id="NumberOfPropertyTxt<?php echo $id;?>" value=""/>

When my ajax is calling i got all the value in it..

var values=$('input[name="NumberOfProperty[]"]').map(function()
{
return this.value
}).get();
console.log(values);

In this i got my all price stored in to the array...then

var Total=0;
                        $('input[name="NumberOfProperty[]"]').each(function()
{
if($(this).val()>0)
{
Total+= parseFloat($(this).val());
}
});
$('#sub_total').val(Total);
$('#subtotal').html(Total);

I got all the value into the Total and assign it in to the sub_total id.    
like image 50
aliasgar vanak Avatar answered Sep 24 '22 20:09

aliasgar vanak