Write a single PHP document that runs script a second time to retrieve inputs

I need to create a table made up of pizza prices obtained programmatically from an array along with an order form. I did that part ok. However, upon submitting the order the script must run again and generate a recipt for purchase indicating the kind of pizza, size, price, tax and total. So all code must be obtained in the same script. This is the part I’m having a problem with. I cannot create another PHP file to place in the form action. Please help. Below is my code thus far. Thank you

Pizza Central

<?php $prices = array( array("Plain", "$3.50", "6.25", "$8.00"), array("Vegetarian", "$4.35", "$7.60", "$12.00"), array("Pepperoni", "$7.25", "$10.75", "$14.00"), array("Hawaiian", "$8.00", "$12.50", "$15.50"), ); ?> <?php foreach($prices as $price) { echo ''; foreach($price as $item) { echo ""; } echo ''; } ?>
Type of Pizza Small Medium Large
$item

PLACE YOUR ORDER

Type of Pizza Plain Vegetarian Pepperoni Hawaiian
Size of Pizza Small Medium Large
<br><input type="submit" 
          value="Place your order"><br>

You can issue receipt in the same part of code where you are submitting order. Or, you can save this info into the database, and use record id to pass to the next script (either via GET method, or if you wish to hide this from user, save this id into $_SESSION or $_COOKIE).

Have a look at this and tell me if its what you were going for. I have commented the script to help you see what I have done.

[php]

<?php $prices = array( array("$3.50", "6.25", "$8.00"), array("$4.35", "$7.60", "$12.00"), array("$7.25", "$10.75", "$14.00"), array("$8.00", "$12.50", "$15.50"), ); //isset statement checks if the form has been posted, if it has then the recipt is shown, if not then the form is shown if(isset($_POST['pizzaTypeDropdown'] { //you can construct a recipt between here and the else clause $type = $_POST['pizzaTypeDropdown']; $size = $_POST['pizzaSizeDropdown']; //say you chose Small Hawaiian... the $type will be 4 and $size will be 1... therefore $price will be set as subarray 4, item 1 ($8.00) $price = $prices[$type][$size]; //works out the total (in the UK tax is 17.5% therefore change 17.5 to whatever tax is where you are) $total = (((17.5/$price)*100) + $price) echo "Thank You for Ordering a $size $type Pizza. That will be $".$total.""; } else { ?>

Pizza Central

<?php foreach($prices as $price) { echo ''; foreach($price as $item) { echo ""; } echo ''; } ?>
Type of Pizza Small Medium Large
$item

PLACE YOUR ORDER

Type of Pizza Plain Vegetarian Pepperoni Hawaiian
Size of Pizza
 <select name="pizzaSizeDropdown">
  <option value="1">Small</option>
  <option value="2">Medium</option>
  <option value="3">Large</option>
</select>

<br><input type="submit" 
          value="Place your order"><br>
<?php } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service