JustAskingThings
JustAskingThings

Reputation: 315

Checking all $_POST values for answer and printing

I am creating an online grocery site where a user can enter his/her full name & address and then proceed to purchase groceries.

There are 20 grocery items to choose from - if the user wants an item, they can simply enter how many units of that item they want; this is the html code for for just 1 of the 20 items.

        <tr>
           <td> Milk - $3.99/carton </td>
           <td> <input type="number" name="amtMilk" min=0> </td>
        </tr>

At the bottom of the page there is a submit button which leads to a confirmation page in php. The confirmation page outputs the users name, address, all the items ordered and a total before and after tax.

I have written out the PHP for this however, it doesn't seem to be working correctly. Below is my code shortened to 4 items:

<?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"; 

?>

Can anyone point out what i'm doing wrong or how I can fix this so get the desired output?

Upvotes: 2

Views: 97

Answers (3)

smcjones
smcjones

Reputation: 5600

Fun fact: PHP turns elements with names structured like arrays into PHP arrays.

So, you can do this:

<?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
}

I would consider either passing your price as a hidden field (for example, with the name price[milk]), or ensuring your array is available after you've submitted the form like I have done above. That way you don't have to hard-code in prices. The way you have it, it's going to be a nightmare to change if the prices change!

To add a new item, all you need to do is add a new key/array pair to the $array. No additional coding on the back-end. Just results.

Check it out here.

Upvotes: 2

Mubin
Mubin

Reputation: 4445

you're doing many things wrong.

so, how are you trying to display html inside php without using print/echo?

So here's revised code, hope this will resolve your issues.

<?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"; 

?>

EDIT

Comment out the foreach($POSTvalues as $key) and change all elseif to if.

add another condition in if statement like this && $_POST['amtCereal'] > 0 to ensure that it has value greater than 0

Upvotes: 1

adamdewolf
adamdewolf

Reputation: 21

There is no need for the foreach loop, and thus no need for the $POSTvalues array.

Use independent if statements without the elseif.

A little psuedocode...

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
}

Upvotes: 2

Related Questions