Calculator Script
Posted: Mon Feb 03, 2014 5:15 pm
I'm new to this programing lark and I was wondering if someone could help.
I have created a script that calculates widgets. You have to enter a value and amount and the script displays the results.
Lets assume that we wanted the user to select from dropdown boxes. I.E. We have 3 types of widgets (Small, medium and large) Small widgets are say £ 50.00, medium are £100.00 and large are £ 150.00. You can only order these widgets in multiples of ten, say upto 50 max. E.G 10, 20, 30, 40, 50. (Hope I have explained that right).
I know how to create dropdown boxes with select values but not sure how to change this script to incorporate them.
Any help would be apreciated
Peter
I have created a script that calculates widgets. You have to enter a value and amount and the script displays the results.
Lets assume that we wanted the user to select from dropdown boxes. I.E. We have 3 types of widgets (Small, medium and large) Small widgets are say £ 50.00, medium are £100.00 and large are £ 150.00. You can only order these widgets in multiples of ten, say upto 50 max. E.G 10, 20, 30, 40, 50. (Hope I have explained that right).
I know how to create dropdown boxes with select values but not sure how to change this script to incorporate them.
Any help would be apreciated
Peter
Code: Select all
<?php # Script 3.10 - calculator.php #4
$page_title = 'Widget Cost Calculator';
include ('../includes/header.html');
// This function calculates the total and prints the results. We have made the $tax argument optional for the user. It now has a default value.
function calculate_total ($qty, $cost, $tax = 5) {
$total = ($qty * $cost);
$taxrate = ($tax / 100); // Turns 5% into 05.
$total += ($total * $taxrate); // Add the tax
return number_format($total, 2);
} // end of function
// Check the form submission
if (isset($_POST['submitted'])) {
// Minimal form submission
if ( is_numeric($_POST['quantity']) && is_numeric($_POST['price']) ) {
// Print the heading
echo '<h1>Total Cost</h1>';
// Call the function with or without tax:
if (is_numeric($_POST['tax'])) {
$sum = calculate_total ($_POST['quantity'], $_POST['price'], $_POST['tax']);
} else {
$sum = calculate_total ($_POST['quantity'], $_POST['price']);
}
// print the results:
echo '<p>The total cost of purchasing ' . $_POST['quantity'] . ' Widget(s) at £' . number_format ($_POST['price'], 2) . ' each, with tax, is £' . $sum . '.</p>';
} else { // invalid submitted values:
echo '<h1>Error!</h1>
<p class="error">Please enter a valid quantity and price.</p>';
}
} // end of main isset() IF.
// Leave the PHP section and create the HTML form:
?>
<h1>Widget Cost Calculator</h1>
<form action="calculator4.php" method="post">
<p>Quantity: <input type="text" name="quantity" size="5" maxlengh="5" value="<?php if(isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></p>
<p>Price: <input type="text" name="price" size="5" maxlengh="10" value="<?php if(isset($_POST['price'])) echo $_POST['price']; ?>" /></p>
<p>Tax (%): <input type="text" name="tax" size="5" maxlengh="5" 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 the footer:
include ('../includes/footer.html');
?>