Sohail Younas Sohail Younas - 2 months ago 11
Ajax Question

i am making Point of Sale and i want to get nettotal of the products, using ajax but i am having trouble in it

this is my javascript




function multiply(id) {

var quantity = $("#quantity"+id).val();

var price = $("#price"+id).text();



var dataArray= 'quantity1='+ quantity + '&price1='+ price;
$.ajax({
url: "ajaxmultiply.php",
data: dataArray,
type: 'POST',

success: function (data) {
$("#total"+id).(data);

}

});

}


</script>


i am able to get the total of the products but i am having issue in getting nettotal of the product, i am doing this using ajax, when i add the total it will give me previous value as concatenated with next value,i tried using through array but it also didnt work, now i store the total value in new table and then fetch it from there and add it but i didnt give me required answer, i tried to use array in it but it also didnt work for me

This is my ajaxmultiply.php file code

<?php
include 'connection.php';
$quantity=$_POST['quantity1'];
$price = $_POST['price1'];
$total= $quantity * $price;

//echo $total;
$query =mysqli_query ($conn,"INSERT INTO grand_total (total) VALUES
('$total')");

$getdata=mysqli_query($conn,"SELECT * FROM grand_total");
while ($fetchdata=mysqli_fetch_assoc($getdata)){
$newtotal = $fetchdata['total'];
$id = $fetchdata['id'];
$grandTotal=$newtotal + $newtotal;

echo $grandTotal;
}

Answer Source

Your while loop should be like below:

$grandTotal = 0;
while ($fetchdata=mysqli_fetch_assoc($getdata)){
    $newtotal = $fetchdata['total'];
    $id = $fetchdata['id'];
  $grandTotal += $newtotal;

}

echo $grandTotal;