Find the Quantity, Unit Price and Total Cost Shopping Cart Help

Discussion in 'PHP' started by paul_whiting09, May 27, 2014.

  1. #1
    Hello I am having some difficulty with my shopping cart script. So far I have it so you can update how many quantities of the item you want and it also updates the price as well. I want it so that once you click a button that says "Add To Cart" it sends you to a page that displays your order and tells you the Quantity, Unit Price and Total Cost of it.

    Below is the code I have so far.

    Javascript:
    
    <script type="text/javascript">
    
    $(document).ready(function() {
    
                     
    
    $("#Quantity").keypress(function(event) {
    
    return /\d/.test(String.fromCharCode(event.keyCode));
    
                                                      });
    
                     
    
    $("#Quantity").change(function(event) {
    
    $(this).val(Math.abs($(this).val()));
    
                       
    
    UpdatePrice();
    
    });
    
                     
    
    $("#CartForm").submit(function(event) {
    
                         
    
    if(!isNormalInteger($("#Quantity").val()))
    
    {
    
    event.preventDefault();
    
                         
    
    alert("Sorry, you must select at least one of these DVD's!");
    
             
    
    return false
    
    }
    
    });
    
                     
    
    UpdatePrice();
    
    });
    
    
    
    function UpdatePrice()
    
    {
    
    var AdjustedPrice = parseFloat($("#BaseUnitPrice").val())*parseInt($("#Quantity").val());
    
                                 
    
    $("#AdjustedPrice").html(AdjustedPrice.toFixed(2) + $("#Currency").val());
    
                              }
    
    
    
    function ChangeQuantity(Value)
    
    {
    
    var NewQuantity = parseInt($("#Quantity").val()) + parseInt(Value);
    
    
    
    if(NewQuantity < 1)
    
    NewQuantity = 1;
    
    
    
    $("#Quantity").val(NewQuantity);
    
    
    
    UpdatePrice();
    
    }
    
    
    
    function isNormalInteger(str) {
    
    return /^\+?(0|[1-9]\d*)$/.test(str);
    
    }
    
    </script>
    
    Code (markup):
    PHP:

    
    echo'<div class="right"><img alt="Picture" src="dvdcovers/wolverine.jpg" height="100" width="100" /></div>';
    
    echo'<font color="white">';
    
    echo'<br />';
    
    echo'<b>Title:</b> The Wolverine';
    
    echo'<br />';
    
    echo'<b>Quantity:</b> $Quantity;';
    
    echo'<br />';
    
    echo'<b>Unit Price:</b>';
    
    echo'<br />';
    
    echo'<b>Total Cost:</b>';
    
    echo'</font>';
    
       
    
    echo'<br /><br />';
    
    echo'<h3 class="widget-title">Purchasing And Payment</h3>';
    
    echo'<form action="cart.php" method="post" id="CartForm">';
    
       
    
    echo'<b>Unit Price: </b><div id="AdjustedPrice"></div>';
    
    echo'<br />';
    
    echo'<input type="hidden" id="Currency" value=" AUD" />';
    
    
    
    echo'<input type="hidden" id="BaseUnitPrice" name="UnitPrice" value="29.99" />';
    
    
    
    echo'<input type="hidden" name="ProductCode" value="000" />';
    
    echo'<br />';
    
    echo'<input type="text" id="Quantity" name="Quantity" value="1" />';
    
    echo'<input type="button" value="-" onclick="ChangeQuantity(-1);" />';
    
    echo'<input type="button" value="+" onclick="ChangeQuantity(1);" />';
    
    echo'<br />';
    
    echo'<input type="Submit" value="Buy" />';
    
    echo'</form>';
    
    Code (markup):
    Any help would be much appreciated. Thankyou.
     
    paul_whiting09, May 27, 2014 IP