简体   繁体   中英

Find the Quantity, Unit Price and Total Cost Shopping Cart

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>

HTML/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>';

Basically I would like to know how to get the value of quantity, unit price and total cost to a new php page that displays these details. Any help would be great. Thankyou.

Have you tried storing variables in a session after you submit the form?

/* 
   Cart.php
*/   

// start session
session_start();

// store session data
$_SESSION['Quantity'] = $_POST['Quantity'];
$_SESSION['UnitPrice'] = $_POST['UnitPrice'];
$_SESSION['TotalCost'] = $_SESSION['UnitPrice'] * $_SESSION['Quantity'];


echo "<br>Quantity =>  " .'$'. $_SESSION['Quantity'];
echo "<br>UnitPrice => " .'$'. $_SESSION['UnitPrice'];
echo "<br>TotalCost => " .'$'. $_SESSION['TotalCost'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM