Shopping cart - auto change quantity, total and grandtotal
Posted: Wed May 27, 2009 9:21 pm
Hey, i have a question about how to make the total and grand total change when i change the quantity. My code can change only total, but grand total din change as well. Besides, only first item that i add, can function. For the second item or third item, when i change the quantity, the total of first item change.
I know javascript just refresh on client site but not server site. So is it any solution to do so?
This is my javascript:
This is my php code:
Hope you all can help me^^ Thank you!
I know javascript just refresh on client site but not server site. So is it any solution to do so?
This is my javascript:
Code: Select all
function cal(x)
{
var qty = parseFloat(document.getElementById(x).value);
var price = parseFloat(document.getElementById('price').value);
var tran = parseFloat(document.getElementById('tran').value);
var total = (qty*(price + tran));
var total2 = total.toFixed(2);
document.getElementById('total').value = total2;
document.getElementById(this).value = qty;
}
Code: Select all
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
$number = 0;
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<script src="calculate.js" type="text/javascript"></script>';
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table border="0" cellspacing="0" cellpadding="3" >';
$output[] = '<tr align="center">';
$output[] = '<td>No.</td>';
$output[] = '<td>Product</td>';
$output[] = '<td>Quantity</td>';
$output[] = '<td>Unit Price (RM)</td>';
$output[] = '<td>Transportation (RM)</td>';
$output[] = '<td>Total (RM)</td>';
$output[] = '<td>Action</td></tr>';
foreach ($contents as $id=>$qty) {
$sql = "SELECT * FROM product_items WHERE id = '$id'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$prodId = $row['productId'];
$qry = "SELECT name FROM products WHERE id='$prodId'";
$res = mysql_query($qry);
$dis = mysql_fetch_array($res);
$number ++;
$output[] = '<tr>';
$output[] = '<td>'.$number.'</td>';
$output[] = '<td>'.$dis['name']." ".$row['model'].'</td>';
$output[] = '<td><input type="text" id="qty'.$id.'" name="qty'.$id.'" value="'.$qty.'" size="1" maxlength="3" onkeyup="return cal(this.id)"></td>';
$output[] = '<td><input type="hidden" size="4" name="price'.$id.'" id="price'.$id.'" value="'.$row['price1'].'">'.$row['price1'].'</td>';
$output[] = '<td><input type="hidden" size="3" name="tran'.$id.'" id="tran'.$id.'" value="'.$row['transportation1'].'">'.$row['transportation1'].'</td>';
$total = (($row['price1'] * $qty)+($row['transportation1'] * $qty));
$output[] = '<td><input type="text" readonly="readonly" size="4" name="total" id="total" value="'.number_format($total,2).'"></td>';
$gtotal += (($row['price1'] * $qty)+($row['transportation1'] * $qty));
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'">Remove</a></td>';
$output[] = '</tr>';
}
$output[] = '</tr>';
$output[] = '<tr><td colspan="6" align="right">Grand total: <strong>RM '.number_format($gtotal,2).'</strong></td></tr>';
$output[] = '<tr><td></td></tr>';
$output[] = '</table>';
$output[] = '</form>';