Wizards,

I am VERY new to PHP, paging through Meloni's 'PHP Essentials' I get the following 
error which I assume is very simple to solve, but for me. I have tried different 
approaches in identifying the 'price' variable with no luck:

error:

Notice: Undefined variable: price in c:\inetpub\wwwroot\php exercises\do_calculate.php 
on line 20

Notice: Undefined variable: price in c:\inetpub\wwwroot\php exercises\do_calculate.php 
on line 24

You ordered 1 bags of Ethopian Harrar.

Bags of Ethopian Harrar are R0.00 each.

Your subtotal is R0.00.

Sales tax is 14% in this location.

R0.00 has been added to your order.

You owe R0.00 for your coffee.

code:

show_calculate_b.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Bean Counter Form</TITLE>
</HEAD>
<BODY>
<FORM method="POST" action="do_calculate.php">
<P>Select a bean type:</P>
<SELECT name="beans" size="1">
 <OPTION value="Ethiopian Harrar">Ethiopian Harrar - $14.25</OPTION>
 <OPTION value="Kona">Kona - $16.25</OPTION>
 <OPTION value="Sumatra">Sumatra - $13.00</OPTION>
</SELECT>
<P>How many bags would you like?</P>
<SELECT name="quantity" size="1">
 <OPTION value="1">1</OPTION>
 <OPTION value="2">2</OPTION>
 <OPTION value="3">3</OPTION>
 <OPTION value="4">4</OPTION>
 <OPTION value="5">5</OPTION>
</SELECT>
<INPUT type="submit" value="Submit">
</FORM>
</BODY>
</HTML>

do_calculate_b.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Bean Counter Results</TITLE>
</HEAD>
<BODY>
<?php
// set up the pricing assignments
if ($_POST[beans] == "Ethiopian Harrar") {
 $price = 14.25;
} else if ($_POST[beans] == "Kona") {
 $price = 16.25;
} else if ($_POST[beans] == "Sumatra") {
 $price = 13.00;
}
$sales_tax = .0825;
$sub_total = $price * $_POST[quantity];
$sales_tax_amount = $sub_total * $sales_tax;
$sales_tax_pct = $sales_tax * 100;
$grand_total = $sub_total + $sales_tax_amount;
$fmt_price = sprintf("%0.2f",$price);
$fmt_sub_total = sprintf("%0.2f",$sub_total);
$fmt_sales_tax_amount = sprintf("%0.2f",$sales_tax_amount);
$fmt_grand_total = sprintf("%0.2f",$grand_total);
echo "<P>You ordered $_POST[quantity] bags of $_POST[beans].</p>";
echo "<P>Bags of $_POST[beans]  are \$$fmt_price each.</p>";
echo "<P>Your subtotal is \$$fmt_sub_total.</p>";
echo "<P>Sales tax is $sales_tax_pct% in this location.</p>";
echo "<P>\$$fmt_sales_tax_amount has been added to your order.</p>";
echo "<P>You owe \$$fmt_grand_total for your coffee.</p>";
?>
</BODY>
</HTML>

Regards and many thanks,

Will

Reply via email to