检查所有 $_POST 值以进行应答和打印
Checking all $_POST values for answer and printing
我正在创建一个在线杂货网站,用户可以在其中输入 his/her 全名和地址,然后继续购买杂货。
有 20 种杂货可供选择 - 如果用户想要某件商品,他们只需输入他们想要的该商品的数量;这是 20 个项目中的 1 个的 html 代码。
<tr>
<td> Milk - .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 数组。
使用 independent 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
}
你做错了很多事。
那么,您如何在不使用 print/echo
的情况下在 php
中显示 html
?
这是修改后的代码,希望这能解决您的问题。
<?php
echo '<h2> Customer Details: </h2>';
echo '<p>Customer Name: </p>'. $_POST['Name'];
echo '<p>Address: </p>'. $_POST['Address'];
$total = 0;
echo '<p>You ordered: </p>';
$POSTvalues = array('amtMilk', 'amtEggs', 'amtBread', 'amtCereal');
//foreach($POSTvalues as $key)
{
if (isset($_POST['amtMilk']) && $_POST['amtMilk'] > 0) {
$total+= 3.99*($_POST['amtMilk']);
echo "Milk";
}
if (isset($_POST['amtEggs']) && $_POST['amtEggs'] > 0) {
$total+= 2.99*($_POST['amtEggs']);
echo "Eggs";
}
if (isset($_POST['amtBread']) && $_POST['amtBread'] > 0) {
$total+= 1.50*($_POST['amtBread']);
echo "Bread";
}
if (isset($_POST['amtCereal']) && $_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 />";
echo "<h3> GRAND TOTAL: </h3>$afterDelivery";
?>
编辑
注释掉 foreach($POSTvalues as $key)
并将所有 elseif
更改为 if
。
在 if
语句中添加另一个条件,这样 && $_POST['amtCereal'] > 0
以确保它的值大于 0
有趣的事实:PHP 将 name
结构类似于数组的元素转换为 PHP 数组。
所以,你可以这样做:
<?php
$array = array(
"milk" => array(
"price" => 3.99,
"unit" => "carton",
),
"eggs" => array(
"price" => 2.99,
"unit" => "dozen",
),
);
if (isset($_POST['amt'])) {
var_dump($_POST['amt']);
$total = 0;
foreach ($_POST['amt'] as $name=>$num) {
if($num > 0) {
$price = $array[$name]['price'];
$amt = $_POST['amt'];
$total += $price * $num;
echo $num . " " . ucfirst($name) . ", ";
}
}
echo "<br />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 />";
echo '<form method="POST"><button type="submit">Back</button></form>';
} else {
?><form method="POST"><table><?php
foreach ($array as $name=>$item) {
?>
<tr>
<td> <?php echo ucfirst($name); ?> - $<?php echo $item['price']; ?>/<?php echo $item['unit']; ?> </td>
<td> <input type="number" name="amt[<?php echo $name; ?>]" min=0> </td>
</tr><?php
}
?></table><input type="submit"></form><?php
}
我会考虑将您的价格作为隐藏字段传递(例如,使用名称 price[milk]
),或者确保您的数组在您像我上面那样提交表单后可用。这样您就不必对价格进行硬编码。你拥有它的方式,如果价格改变,改变将是一场噩梦!
要添加新项目,您需要做的就是将新的 key/array 对添加到 $array
。后端无需额外编码。只是结果。
看看here。
我正在创建一个在线杂货网站,用户可以在其中输入 his/her 全名和地址,然后继续购买杂货。
有 20 种杂货可供选择 - 如果用户想要某件商品,他们只需输入他们想要的该商品的数量;这是 20 个项目中的 1 个的 html 代码。
<tr>
<td> Milk - .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 数组。
使用 independent 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
}
你做错了很多事。
那么,您如何在不使用 print/echo
的情况下在 php
中显示 html
?
这是修改后的代码,希望这能解决您的问题。
<?php
echo '<h2> Customer Details: </h2>';
echo '<p>Customer Name: </p>'. $_POST['Name'];
echo '<p>Address: </p>'. $_POST['Address'];
$total = 0;
echo '<p>You ordered: </p>';
$POSTvalues = array('amtMilk', 'amtEggs', 'amtBread', 'amtCereal');
//foreach($POSTvalues as $key)
{
if (isset($_POST['amtMilk']) && $_POST['amtMilk'] > 0) {
$total+= 3.99*($_POST['amtMilk']);
echo "Milk";
}
if (isset($_POST['amtEggs']) && $_POST['amtEggs'] > 0) {
$total+= 2.99*($_POST['amtEggs']);
echo "Eggs";
}
if (isset($_POST['amtBread']) && $_POST['amtBread'] > 0) {
$total+= 1.50*($_POST['amtBread']);
echo "Bread";
}
if (isset($_POST['amtCereal']) && $_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 />";
echo "<h3> GRAND TOTAL: </h3>$afterDelivery";
?>
编辑
注释掉 foreach($POSTvalues as $key)
并将所有 elseif
更改为 if
。
在 if
语句中添加另一个条件,这样 && $_POST['amtCereal'] > 0
以确保它的值大于 0
有趣的事实:PHP 将 name
结构类似于数组的元素转换为 PHP 数组。
所以,你可以这样做:
<?php
$array = array(
"milk" => array(
"price" => 3.99,
"unit" => "carton",
),
"eggs" => array(
"price" => 2.99,
"unit" => "dozen",
),
);
if (isset($_POST['amt'])) {
var_dump($_POST['amt']);
$total = 0;
foreach ($_POST['amt'] as $name=>$num) {
if($num > 0) {
$price = $array[$name]['price'];
$amt = $_POST['amt'];
$total += $price * $num;
echo $num . " " . ucfirst($name) . ", ";
}
}
echo "<br />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 />";
echo '<form method="POST"><button type="submit">Back</button></form>';
} else {
?><form method="POST"><table><?php
foreach ($array as $name=>$item) {
?>
<tr>
<td> <?php echo ucfirst($name); ?> - $<?php echo $item['price']; ?>/<?php echo $item['unit']; ?> </td>
<td> <input type="number" name="amt[<?php echo $name; ?>]" min=0> </td>
</tr><?php
}
?></table><input type="submit"></form><?php
}
我会考虑将您的价格作为隐藏字段传递(例如,使用名称 price[milk]
),或者确保您的数组在您像我上面那样提交表单后可用。这样您就不必对价格进行硬编码。你拥有它的方式,如果价格改变,改变将是一场噩梦!
要添加新项目,您需要做的就是将新的 key/array 对添加到 $array
。后端无需额外编码。只是结果。
看看here。