我是PHP的新生。当有人购买100美元以上时,我想给予20%的折扣。
这是我当前的代码,显示唯一的商品价格和总价格,包括增值税。
HTML:
<form action="sweet1.php" method="post">
Item Name: <input type="text" value="" style="display: none;">
<select name="sweet">
<option value="Item one@250">Item One USD 250</option>
<option value="Item Two@180">Item Two USD 180</option>
</select>
<br><br>
Quantity: <input type="text" name="qty" value="">
<br><br>
<input type="submit" value="Purchase">
</form>
PHP:
<?php
$sweet_name=$_POST['sweet'];
$qty=$_POST['qty'];
$position=strpos($sweet_name, '@');
echo $position;
$sweet=substr($sweet_name,0,$position);
$price=substr($sweet_name,$position+1);
echo 'Sweet Details';
echo '<br>Item Name: '.$sweet;
echo '<br>Qty: '.$qty;
echo '<br>Unique Item Price: '.$price;
echo '<br>Total Iteam Price: '.$price*$qty;
$tprice=$price*$qty;
$vat= $tprice*.10;
$tamount=$tprice+$vat;
echo '<br> Total Amount (VAT Included): '.$tamount;
?>
答案 0 :(得分:1)
您可以尝试使用以下代码来避免DOM更改。
<form action="sweet1.php" method="post">
Item Name: <input type="text" value="" style="display: none;">
<select name="sweet">
<option value="item_1">Item One USD 250</option>
<option value="item_2">Item Two USD 180</option>
</select>
<br><br>
Quantity: <input type="text" name="qty" value="">
<br><br>
<input type="submit" value="Purchase">
</form>
PHP脚本:
<?php
$sweet_arr = array(
"item_1" => array(
"name" => "Item one@250",
"price" => "250"
),
"item_2" => array(
"name" => "Item Two@180",
"price" => "180"
)
);
$sweet_name = $_POST['sweet'];
$qty = $_POST['qty'];
try {
if (!is_array($sweet_arr[$sweet_name])) {
throw new Exception("Item not found.");
}
$sweet = $sweet_arr[$sweet_name]['name'];
$price = $sweet_arr[$sweet_name]['price'];
if (empty($price)) {
throw new Exception("Price not defined.");
}
echo 'Sweet Details';
echo '<br>Item Name: ' . $sweet;
echo '<br>Qty: ' . $qty;
echo '<br>Unique Item Price: ' . $price;
echo '<br>Total Iteam Price: ' . $price * $qty;
$tprice = $price * $qty;
$vat = $tprice * .10;
if ($tprice > 500) {
$discount = $tprice * .20;
$tprice -= $discount;
}
$tamount = $tprice + $vat;
echo '<br> Total Amount (VAT Included): ' . $tamount;
} catch (Exception $e) {
echo $e->getMessage();
}
答案 1 :(得分:0)
试试这个:
$sweet_name=$_POST['sweet'];
$qty=$_POST['qty'];
$position=strpos($sweet_name, '@');
echo $position;
$sweet=substr($sweet_name,0,$position);
$price=substr($sweet_name,$position+1);
echo 'Sweet Details';
echo '<br>Item Name: '.$sweet;
echo '<br>Qty: '.$qty;
echo '<br>Unique Item Price: '.$price;
echo '<br>Total Iteam Price: '.$price*$qty;
$tprice=$price*$qty;
$vat= $tprice*.10;
$tamount=$tprice+$vat;
$discount = 0;
if ($tprice > 500) {
$discount = $tprice * 20/100;
}
$tamount -= $discount;
echo '<br> Total Amount (VAT Included): '.$tamount;
答案 2 :(得分:-1)
如果您希望在总金额为500美元的情况下购买20%,则添加以下代码
if ($tamount > 500)
{
$discount = $tamount*20/100;
$tamount = $tamount - $discount;
}