简体   繁体   English

查找数量,单价和总成本购物车

[英]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: 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: 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. 基本上,我想知道如何获取数量,单价和总成本的值到显示这些详细信息的新php页面。 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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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