It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

JavaScript display quantity help

JavaScript display quantity help

Postby desmondchin on Thu Oct 20, 2011 6:18 am

Hi everyone... i'm new to JavaScript. i'm stuck with my coding please any help will appreciate.
My problem with the coding is, when user enter the amount inside quantity box, the total amount will x the quantity(example , quantity 2 x MYR72 = MYR144) but the amount wont show up at sub-total. Other calculation like shipping cost & total (shipping cost + sub-total) all working fine. sorry for bad English & thank you in advanced.

Code: Select all
<?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
var price = 0;
var state = document.frmCart.state.value;

//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('ct_qty').innerText; // pd_price * ct_qty



//document.frmCart.amount.innerText=price;
document.getElementById('amount').innerText =  price;
//alert("asdf");
var test = document.getElementById('subtotal').innerText;
//alert(test);
test = test.substring(3);
//alert(test);
document.getElementById('total').innerText =   price + parseInt(test);
}
//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/btn_home_roll.gif','../images/btn_about_roll.gif','../images/btn_bloomingcare_roll.gif','../images/btn_promotion_roll.gif','../images/btn_contactus_roll.gif'), doMath()" >



<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><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"><?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 Malaysia</option>
                    <option value="west">Peninsular Malaysia</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="132" 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="121" 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="132" 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 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>


</body>
</html>

desmondchin
 
Posts: 1
Joined: Thu Oct 20, 2011 6:14 am

Who is online

Users browsing this forum: No registered users and 8 guests