PHP cookies help

Dear all, i need help so badly…urgent

i have customize a simple e-comm site (from www.phpwebcommerce.com tutorial), when user go checkout page(cart.php) it will show product item, quantity, sub-total , shipping, state and total price. Original from the tutorial using php do calculation but shipping cost is flat rate, i need to calculate shipping cost by state & quantity so that i use JavaScript do the calculation.

my shopping cart process :

  1. user checkout - go to cart.php (user can update qunatity and select state from this page then update cart) this page is working fine…
  2. user enter info - user enter payment and shipping address
  3. user continue checkout - go to checkoutconfirmation.php (my problem appear here, the information like quantity, sub-total, shipping cost & total from cart.php won’t update here)

I’m trying not do confuse myself & do it the eaiser way by using PHP cookies to send the javascript value tocheckoutconfirmation.php but i’m not sure how it work…i’m new to php and javascript, any help will appeciated ! Thank you in advanced !!

cart.php


<?php
require_once 'library/config.php';
require_once 'library/cart-functions.php';

$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';

switch ($action) {
	case 'add' :
		addToCart();
		break;
	case 'update' :
		updateCart();
		break;	
	case 'delete' :
		deleteFromCart();
		break;
	case 'view' :
}

$cartContent = getCartContent();
$numItem = count($cartContent);

$pageTitle = 'Shopping Cart';
require_once 'include/header.php';

// show the error message ( if we have any )
displayError();

if ($numItem > 0 ) {
?>
<head>


 <script type="text/javascript">
 
<!-- Begin
function  doMath() {
var one = eval(document.getElementById('txtQty[]').value) //one = quantity 
//alert(one);
var price = 0;
var state = document.frmCart.state.value;
///////var pd_price = 72; //////////////

var unitPrice = document.getElementById('pd_price').innerText.substring(3);
//alert(unitPrice);
//alert(one);

if(state == "east"){
	if (one == 1){
		price = 6;
	}
	else if (one == 2){
		price = 8;
	}
	else if (one == 3){
		price = 10;
	}
	else if (one == 4){
		price = 12;
	}
	else if (one == 5){
		price = 12.50;
	}
	else if (one == 6){
		price = 13;
	}
	else if (one == 7){
		price = 14;
	}
	else if (one == 8){
		price = 0;
	}
	
}

else if(state == "west"){
	if (one == 1){
		price = 9.30;
	}
	if (one == 2){
		price = 12;
	}
	if (one == 3){
		price = 17.25;
	}
	if (one == 4){
		price = 20;
	}
	if (one == 5){
		price = 21;
	}
	if (one == 6){
		price = 22;
	}
	if (one == 7){
		price = 23.45;
	}
	if (one == 8){
		price = 0;
	}
}


document.getElementById('subtotal').innerText =  "MYR" +  unitPrice * one;

//document.frmCart.amount.innerText=price;
document.getElementById('amount').innerText =  price; ///amount = shipping
//alert("asdf");
var test = document.getElementById('subtotal').innerText; // = one*pd_price; //													
//alert(test);
//alert(one);
test = parseInt(test.substring(3)) * one;
//alert(test);
//document.getElementById('subtotal').innerText = test;
//alert(test);
//alert("A" + price);
//alert(parseInt(test));
//alert(one);
document.getElementById('total').innerText =   price + unitPrice * one; //total = shipping + subtotal

test = "MYR" + test;

document.getElementById('ct_qty').innerText =  document.getElementById('subtotal').innerText;
}
//function custRound(x,places) {
//return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
//}
//  End -->s
	
	
</script>
</head>


<body onLoad="MM_preloadImages('../images/'), doMath()" >



<tr><td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td align="center" bgcolor="#b6df51"><table width="800" border="0" cellspacing="0" cellpadding="0"><tr><td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
 
  <tr>
    <td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr><form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"; ?>" method="post" name="frmCart" id="frmCart">
        <td align="center" bgcolor="#b6df51"><table width="800" border="0" cellspacing="0" cellpadding="0" >
          <tr>
            <td align="left"><img src="images/title_purchase_online.gif" width="280" height="25" /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td align="center"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><table width="800" border="0" cellpadding="0" cellspacing="0" bgcolor="#3b7b37">
                  <tr>
                    <td width="15"><img src="images/title_box_left.gif" width="15" height="30" /></td>
                    <td width="770"><table width="770" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="170" align="center" class="yellow_txt"><strong>Item</strong></td>
                        <td width="213" align="center">&nbsp;</td>
                        <td width="141" align="center"><strong class="yellow_txt">Unit Price</strong></td>
                        <td width="106" align="center"><strong class="yellow_txt">Quantity</strong></td>
                        <td width="110" align="center"><strong class="yellow_txt">Total</strong></td>
                        <td width="30" align="center">&nbsp;</td>
                        </tr>
                    </table></td>
                    <td width="15"><img src="images/title_box_right.gif" width="15" height="30" /></td>
                  </tr>
                </table></td>
              </tr>
              
<?php
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
	extract($cartContent[$i]);
	$productUrl = "other_products.php?c=$cat_id&p=$pd_id";
	$subTotal += $pd_price * $ct_qty;
?>
              
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td align="center"><table width="770" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="170" align="center" class="yellow_txt"><table width="150" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                      <tr>                      </tr>
                      <tr>
                        
                        <td width="116" align="center" valign="top" bgcolor="#b6df51"><a href="<?php echo $productUrl; ?>"><img src="<?php echo $pd_thumbnail; ?>" width="116" height="116" border="0" /></a><a href="<?php echo $productUrl; ?>"></a></td>
                      </tr>
                      <tr>                      </tr>
                    </table></td>
                    <td width="220" align="left" class="green_txt"><strong><a href="<?php echo $productUrl; ?>" style="text-decoration: none"><font color="000000"><?php echo  $pd_name; ?></font></a></strong></td>
                    <td width="128" align="center"><strong class="black_txt" id="pd_price"><?php echo displayAmount($pd_price); ?></strong></td>
                    <td width="112" align="center"><input name="txtQty[]"  type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">
                    <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>">
  <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>"></td>
                    <td width="110" align="center"><strong class="black_txt" id="ct_qty"><?php echo displayAmount($pd_price * $ct_qty); ?></strong></td>
                   <td width="30" align="center" valign="middle"><a href="#"> <img src="images/btn_delete.png" width="16" height="16" border="0" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" /></a>                  </tr>
                </table></td>
              </tr>
              
<?php
}
?>

              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td><table width="800" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="13" height="13"><img src="images/text_box_big_left1.gif" width="13" height="13" /></td>
                    <td width="774" height="13" bgcolor="#ffde00"><span class="style10"><img src="images/spacer.gif" width="13" height="13" /></span></td>
                    <td width="13" height="13"><img src="images/text_box_big_right1.gif" width="13" height="13" /></td>
                  </tr>
                  <tr>
                    <td bgcolor="#ffde00">&nbsp;</td>
                    <td height="50" align="center" bgcolor="#ffde00"><table width="770" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="132" align="center" class="yellow_txt">&nbsp;</td>
                        <td width="147" align="left" class="green_txt">&nbsp;</td>
                        <td width="113" align="center">&nbsp;</td>
                        <td width="102" align="center" class="black_txt"><strong>Sub-total</strong></td>
                        <td colspan="2" align="center"><strong class="black_txt" id="subtotal"><?php echo displayAmount($subTotal); ?></strong></td>
                        <td width="19" align="center" valign="middle">&nbsp;</td>
                      </tr>
                      <tr>
                        <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                        </tr>
                      
    
                        
<!-------------------------------------------- shipping cost--------------------------------------------------->
 

 
                      <tr>
                        <td align="center" >&nbsp;</td>
                        <td align="left" class="green_txt">&nbsp;</td>
                        <td> </td>
                        <td align="center" class="black_txt"><strong>State</strong></td>
                        <td colspan="2" align="center">	<select name="state">
        				<option value="east" selected>East </option>
        				<option value="west">Peninsular</option>
      					</select>   </td>
                        <td align="center" valign="middle">&nbsp;</td>
                      </tr>
    
    
    <tr>
                        <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                        </tr>
                         <tr>
                        <td align="center" class="yellow_txt">&nbsp;</td>
                        <td align="left" class="green_txt">&nbsp;</td>
                        <td align="center">&nbsp;</td>
                        <td align="center" class="black_txt"><strong>Shipping</strong></td>
                        
                        <td width="131" align="right"> <strong class="black_txt">
                          MYR</strong><!--<input type="text" name="amount" disabled="disabled" size="17" style="background-color:transparent;" border="none" >--></td>
                        
                        
                        <td width="122" align="left"><strong class="black_txt">
                          <div name="amount" id="amount"> 0</div></strong></td>
                        <td align="center" valign="middle">&nbsp;</td>
                      </tr>
       				<tr>
                        <td align="center" class="yellow_txt">&nbsp;</td>
                        <td align="left" class="green_txt">&nbsp;</td>
                        <td align="center">&nbsp;</td>
                        <td align="center" class="black_txt33">                        </td>
                        <td colspan="2" align="center" class="black_txt33">
                        <font color="#FF0000">*Purchases of 8 boxes onwards free delivery charges.</font>                        </td>
                        <td align="center" valign="middle">&nbsp;</td>
                      </tr>


<!------------------------------------------------------------------------------------------------------>    
                      
                      
                      <tr>
                        <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                        </tr>
                      <tr>
                        <td align="center" class="yellow_txt">&nbsp;</td>
                        <td align="left" class="green_txt">&nbsp;</td>
                        <td align="center">&nbsp;</td>
                        <td align="center" class="black_txt"><strong>Total</strong></td>
                        <td width="131" align="right"> <!--<input type="text" name="amount" disabled="disabled" size="17" style="background-color:transparent;" border="none" >--><strong class="black_txt">
                          MYR</strong></td>
                        
                        
                        <td colspan="2" align="left"><strong class="black_txt" id="total"><?php echo displayAmount($subTotal);// + $shopConfig['shippingCost']); ?></strong>                        </td>
                        
                        <td width="4" align="center" valign="middle">&nbsp;</td>
                      </tr>
                      <tr>
                        <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                        </tr>
                      <tr>
                        <td align="center" class="yellow_txt">&nbsp;</td>
                        <td align="left" class="green_txt">&nbsp;</td>
                        <td align="center">&nbsp;</td>
                        <td align="center">&nbsp;</td>
                        <td colspan="2" align="center"><input type="button" class="update_btn" src="images/btn_update_cart.gif" width="102" height="22" border="0"  name="btnUpdate" id="btnUpdate" onClick="doMath()"><!--value="submit"--></td>
                        <td align="center" valign="middle">&nbsp;</td>
                      </tr>
                    </table></td>
                    <td bgcolor="#ffde00">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="13" height="13"><img src="images/text_box_big_left2.gif" width="13" height="13" /></td>
                    <td bgcolor="#ffde00"><span class="style10"><img src="images/spacer.gif" width="13" height="13" /></span></td>
                    <td width="13" height="13"><img src="images/text_box_big_right2.gif" width="13" height="13" /></td>
                  </tr>
                  <tr>
<td>&nbsp;</td>
</tr>
                </table></td>
              </tr>
              <tr>


<?php

$shoppingReturnUrl = isset($_SESSION['shop_return_url']) ? $_SESSION['shop_return_url'] : 'other_products.php';
?>
              <tr>
             
                <td align="center"><table width="340" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                  <td>&nbsp;</td>
                    <td><a href="#"> <img src="images/btn_continue_shopping.gif" width="162" height="22" border="0" onClick="window.location.href='<?php echo $shoppingReturnUrl; ?>';" /></a></td>
                    
                    
                    
                    
<?php 
if ($numItem > 0) {
?> 

                    <td width="20">&nbsp;</td>
                    <td><a href="#"> <img src="images/btn_continue_checkout.gif" width="162" height="22" border="0" onClick="window.location.href='checkout.php?step=1';"/></a></td>
                    
                  
                   
 <?php
}
?> 
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td align="center">&nbsp;</td>
          </tr>
        </table></td></form>
      </tr>
      
                 

    </table></td>
  </tr>
</table>  
          </tr>
</table></td>
      </tr>
</table></td>
  </tr>


<?php
} else {
	
?>
			 <?php include("EmptyMSG.php"); ?>

			<!----<td><table width="550" bgcolor="#b6df51" border="0" align="center" cellpadding="10" cellspacing="0"><p align="center">You shopping cart is empty</p>
  			 <p>If you find you are unable to add anything to your cart, please ensure that 
   			 your internet browser has cookies enabled and that any other security software 
   			 is not blocking your shopping session.</p>
  			 </table></td>--->
          
      <?php } ?>     
     

</body>
</html>


checkoutConfirmation.php


<?php
/*
Line 1 : Make sure this file is included instead of requested directly
Line 2 : Check if step is defined and the value is two
Line 3 : The POST request must come from this page but the value of step is one
*/
if (!defined('WEB_ROOT')
    || !isset($_GET['step']) || (int)$_GET['step'] != 2
	|| $_SERVER['HTTP_REFERER'] != 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?step=1') {
	exit;
}

$errorMessage = '&nbsp;';

/*
 Make sure all the required field exist is $_POST and the value is not empty
 Note: txtShippingAddress2 and txtPaymentAddress2 are optional
*/
$requiredField = array('txtShippingFirstName', 'txtShippingLastName', 'txtShippingAddress1', 'txtShippingPhone', 'txtShippingState',  'txtShippingCity', 'txtShippingPostalCode',
                       'txtPaymentFirstName', 'txtPaymentLastName', 'txtPaymentAddress1', 'txtPaymentPhone', 'txtPaymentState', 'txtPaymentCity', 'txtPaymentPostalCode');
					   
if (!checkRequiredPost($requiredField)) {
	$errorMessage = 'Input not complete';
}
					   

$cartContent = getCartContent();

?>



</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">&nbsp;</td>
  </tr>
</table>
        
        </td>
      </tr>
    </table></td>
  </tr>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?step=3" method="post" name="frmCheckout" id="frmCheckout">


<table width="900" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td align="center" bgcolor="#b6df51"><table width="800" border="0" cellspacing="0" cellpadding="0"><tr><td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>

  </tr>
    
    
  <tr>
    <td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td align="center" bgcolor="#b6df51">
        
        <!------------------------------COD------------------------------------->
        <?php 
if ($_POST['optPayment'] == 'cod') {
?>


        
        <table width="800" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="330" align="left"><img src="images/title_purchase_online.gif" width="280" height="25" /></td>
            <td width="464" align="left" valign="bottom"><img src="images/title_step02.gif" width="245" height="20" /></td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
            <p id="errorMessage"><?php echo $errorMessage; ?></p>
          </tr>
          <tr>
            <td colspan="2" align="center" class="green_txt"><table width="500" border="0" cellpadding="0" cellspacing="0" bgcolor="#3b7b37">
              <tr>
                <td width="15"><img src="images/title_box_left.gif" width="15" height="30" /></td>
                <td width="470" align="center" class="yellow_txt"><strong>:: IMPORTANT NOTE ::</strong></td>
                <td width="15"><img src="images/title_box_right.gif" width="15" height="30" /></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><img src="images/spacer.gif" width="12" height="12" /></td>
          </tr>
          <tr>
            <td colspan="2" align="center">&nbsp;</td>
            <p id="errorMessage"><?php echo $errorMessage; ?></p>
          </tr>
          
          
          <?php
}
?>
          
       
          <!-----------------paypal--------------------------------->
  <?php 
if ($_POST['optPayment'] == 'paypal') {
?>
          <tr>
            <td colspan="2" align="center"><table width="800" border="0" align="center" cellpadding="10" cellspacing="1" class="infoTable"><tr><td><table width="800" border="0" align="center" cellpadding="10" cellspacing="1" class="infoTable">
              <tr>
              <tr>
                <td width="330" align="left"><img src="images/title_purchase_online.gif" width="280" height="25" /></td>
                <td width="464" align="left" valign="bottom"><img src="images/title_step02.gif" width="245" height="20" /></td>
              </tr>
              
  <td colspan="2" align="center" class="green_txt"><table width="500" border="0" cellpadding="0" cellspacing="0" bgcolor="#3b7b37">
    <tr>
      <td width="15"><img src="images/title_box_left.gif" width="15" height="30" /></td>
      <td width="470" align="center" class="yellow_txt"><strong>:: IMPORTANT NOTE ::</strong></td>
      <td width="15"><img src="images/title_box_right.gif" width="15" height="30" /></td>
    </tr>
  </table></td>
  </tr>
  <td colspan="2" align="center" ><table width="500" border="0" cellpadding="0" cellspacing="0" >
    <tr>
      <td align="center" class="black_txt33"><p>There will be (3.4% + RM2.00 MYR) PayPal transaction fee for credit card payment</p></td>
    </tr>
  </table></td>
  </tr>
         
           
           
            </table></td>
              </tr>
         
  
 
            </table>            </td>
          </tr>
          <?php
}
?>

         
          <!-----------------End of paypal---------------------------->
          
          
          
          
          
          <tr>
            <td colspan="2" align="center"><table width="500" border="0" cellpadding="0" cellspacing="0" bgcolor="#3b7b37">
              <tr>
                <td width="15"><img src="images/title_box_left.gif" width="15" height="30" /></td>
                <td width="470" align="center" class="yellow_txt"><strong>Ordered Item</strong></td>
                <td width="15"><img src="images/title_box_right.gif" width="15" height="30" /></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td colspan="2" align="center">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" align="center"><table width="360" border="0" cellspacing="0" cellpadding="3">
              <tr>
                <td align="left" class="green_txt"><strong>Item</strong></td>
                <td align="right" class="green_txt"><strong>Unit Price</strong></td>
                <td align="right" class="green_txt"><strong>Total</strong></td>
              </tr>
              
              
                  <?php
$numItem  = count($cartContent);
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
	extract($cartContent[$i]);
	$subTotal += $pd_price * $ct_qty;
?>

              
              <tr>
                <td align="left" class="black_txt"><?php echo "$ct_qty x $pd_name"; ?></td>
                <td align="right" class="black_txt"><?php echo displayAmount($pd_price); ?></td>
                <td align="right" class="black_txt"><?php echo displayAmount($ct_qty * $pd_price); ?></td>
              </tr>
              
<?php
}
?>

              
              <tr>
                <td colspan="3" align="left"><img src="images/spacer.gif" width="12" height="12" /></td>
              </tr>
              <tr>
                <td align="left">&nbsp;</td>
                <td align="right" class="green_txt"><strong>Sub-totall</strong></td>
                <td align="right"><span class="black_txt"><?php echo displayAmount($subTotal); ?></span></td>
              </tr>
              <tr>
                <td align="left">&nbsp;</td>
                <td align="right" class="green_txt"><strong>Shipping<br />
                </strong></td>
                <td align="right"><span class="black_txt"><?php echo displayAmount($shopConfig['shippingCost']); ?></span></td>
              </tr>
              <tr>
                <td align="left">&nbsp;</td>
                <td align="right" class="green_txt"><strong>Total</strong></td>
                <td align="right"><span class="black_txt"><?php echo displayAmount($shopConfig['shippingCost'] + $subTotal); ?></span></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td colspan="2" align="center">&nbsp;</td>
          </tr>
           <tr>
            <td colspan="2" align="center">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" align="center" class="black_txt"><strong><?php echo $_POST['optPayment'] == 'paypal' ? 'Paypal' : 'Bank-in / Bank Transfer'; ?>
          <input name="hidPaymentMethod" type="hidden" id="hidPaymentMethod" value="<?php echo $_POST['optPayment']; ?>" /></strong></td>
          </tr>
        
          <tr>
            <td colspan="2" align="center">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" align="center"><table width="380" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td ><input name="btnBack" type="button" id="btnBack"  onClick="window.location.href='checkout.php?step=1';" class="btn" ></td>
                <td width="20">&nbsp;</td>
                <td><input type="image" src="images/btn_confirm_order.gif" width="132" height="22" border="0" value="Submit" name="btnConfirm" id="btnConfirm"></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td colspan="2" align="center">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" align="center">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>  <table width="800" border="0" cellspacing="0" cellpadding="0"><tr></tr>
</table></td>
          </tr>
</table></td>
      </tr>
</table></td>
  </tr>

</form>


</body>
</html>


thank you for the time and attention

How do you call checkoutconfirmation.php? Directly?
Because in cart.php you have some includes that are missing in checkoutconfirmation.php.
And in checkoutconfirmation.php you are using functions like getCartContent() that I’m guessing are defined in the inclused that are missing.

actually i’m not sure can work in this way or not, like after javascript done the calculation…use PHP save the result to checkoutconfirmation.php.
In checkoutconfirmation.php you see the functions getCartContent() is from previous tutorial, that is the problem cause i want to get the information form javascript cart.php, the functions getCartContent() get the data from mysql which is not accurate.

Thank you for helping & sorry for my bad english :slight_smile: