Shopping cart - auto change quantity, total and grandtotal

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Shopping cart - auto change quantity, total and grandtotal

Post by rei27 »

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:

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;
}
 
This is my php code:

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>';
 
Hope you all can help me^^ Thank you!
Last edited by Benjamin on Fri May 29, 2009 10:51 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: Shopping cart - auto change quantity, total and grandtotal

Post by Popcorn »

at a glance it looks like your JavaScript is wrong. look at what you are asking it to set the value of. an element named "total". that's it, it'll do that every time.
Post Reply