我正在创建一个在线杂货网站,用户可以在其中输入他/她的全名和地址,然后继续购买杂货。
有 20 种杂货商品可供选择 - 如果用户想要某种商品,他们只需输入他们想要的该商品的数量即可;这是 20 个项目中仅 1 个项目的 html 代码。
<tr>
<td> Milk - $3.99/carton </td>
<td> <input type="number" name="amtMilk" min=0> </td>
</tr>
页面底部有一个提交按钮,可通往 php 中的确认页面。确认页面输出用户姓名、地址、所有订购的商品以及税前和税后总计。
我已经为此编写了 PHP,但是它似乎无法正常工作。下面是我的代码,缩短为 4 项:
<?php
<h2> Customer Details: </h2>
<p>Customer Name: </p> echo $_POST['Name'];
<p>Address: </p> echo $_POST['Address'];
$total = 0;
<p>You ordered: </p>
$POSTvalues = array('amtMilk', 'amtEggs', 'amtBread', 'amtCereal');
foreach($POSTvalues as $key) {
if ($_POST['amtMilk'] > 0) {
$total+= 3.99*($_POST['amtMilk']);
echo "Milk";
}
elseif ($_POST['amtEggs'] > 0 ) {
$total+= 2.99*($_POST['amtEggs']);
echo "Eggs";
}
elseif ($_POST['amtBread'] > 0 ) {
$total+= 1.50*($_POST['amtBread']);
echo "Bread";
}
elseif ($_POST['amtCereal'] > 0 ) {
$total+= 4.99*($_POST['amtCereal']);
echo "Cereal";
}
}
echo "Your total before Tax is: $total"; <br>
$afterTax = $total*0.13 + $total
$afterDelivery = $afterTax + 3.50
echo "Your total after tax is: $afterTax"; <br>
echo "Your total after delivery is: $afterDelivery";<br>
<h3> GRAND TOTAL: </h3> echo "$afterDelivery";
?>
任何人都可以指出我做错了什么或者我如何解决这个问题以获得所需的输出?
最佳答案
不需要 foreach 循环,因此不需要 $POSTvalues 数组。
使用独立if语句而不使用elseif。
一些伪代码...
if (value1 > 0 )
{
add to total
print item
}
if (value2 > 0 )
{
add to total
print item
}
if (value3 > 0 )
{
add to total
print item
}
if (value4 > 0 )
{
add to total
print item
}
关于php - 检查所有 $_POST 值以进行应答和打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33556303/