calculator

Posted on 16th Feb 2014 by admin

I can't figure out why this code doesn't work. No error messages. Page loads.

Code: <?php # Script 3.9 - calculator.php
$page_title = 'Widget Cost Calculator';
include ('./header.html');



function calculate_total ($tax= 5) {
global $total;
$taxrate = $tax / 100;
$total = ($_POST['quantity'] * $_POST['price']) * ($taxrate + 1);
$total = number_format ($total, 2);
}


if (isset($_POST['submitted'])) {

if (is_numeric($_POST['quantity']) && is_numeric($_POST['price'])) {


echo '<h1 id="mainhead">Total Cost</h1>';

$total = NULL;

if (is_numeric($_POST['tax'])) {
calculate_total ($_POST['tax']);
} else {
calculate_total ();
}

echo '<p>The total cost of purchasing '. $_POST['quantity'] .' widget(s) at $'.
number_format ($_POST['price'], 2). ' each is $'. $total .'.</p>';

echo '<p><br/></p>';

} else {

echo '<h1 id="mainhead">Error!</h1>
<p class="error">Please enter a valid quantity and price.</p><p><br/></p>';
}

}

?>
<h2>Widget Cost Calculator</h2>
<form action="calculator.php" method="post">
<p>Quantity: <input type="text" name="quantity" size="5" maxlength="10" value="<?php if (isset($_POST['quantity'])) echo $_POST['quantity'];?>"/></p>
<p>Price: <input type="text" name="price" size="5" maxlength="10" value="<?php if (isset($_POST['price'])) echo $_POST['price'];?>"/></p>
<p>Tax (%): <input type="text" name="tax" size="5" maxlength="10" value="<?php if (isset($_POST['tax'])) echo $_POST['tax'];?>"/>(optional)</p>
<p><input type="submit" name"submit" value="Calculate!"/></p>
<input type="hidden" name"submitted" value="TRUE"/>
</form>


<?php
include ('./footer.html');
?>The calculator does not work and won't display anything. I've checked the code three times in the text book and I believe their is no typo's. What am I doing wrong?

Other forums