<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Josh Christensen Module 2</title>
</head>
<body>
<?php
$page_title = 'Peters Professional Painters';
function calculate_total ($total, $ptotal, $labortotal = 5) {
$total = ($ptotal + $labortotal);
$ptotal = ($pgallons * $paint);
$labortotal = ($labor * $rate);
$prorate = 17.50;
$regrate = 12.50;
$satin = 31.50;
$flat = 24.00;
$gloss = 27.75;
// Print the results:
echo '<p>The total cost of the paint job is $' . number_format ($total, 2) . '.</p>';
} // End of function.
// Check for form submission:
if (isset($_POST['submitted'])) {
// Minimal form validation:
if ( is_numeric($_POST['ptotal']) && is_numeric($_POST['total']) ) {
// Print the heading:
echo '<h1>Total Cost</h1>';
} else { // Invalid submitted values.
echo '<h1>Error!</h1>
<p class="error">Please enter a valid quantity.</p>';
}
} // End of main isset() IF.
// Leave the PHP section and create the HTML form:
?>
<h1>Peter's Professional Painters</h1>
<form action="index.php" method="post">
<p>Total Square Feet: <input type="text" name="sqfeet" size="5" maxlength="5" value="<?php if (isset($_POST['sqfeet'])) echo $_POST['sqfeet']; ?>" /></p>
<p>Gallons of paint: <input type="text" name="pgallons" size="5" maxlength="10" value="<?php if (isset($_POST['pgallons'])) echo $_POST['pgallons']; ?>" /></p>
<p>Hours of labor: <input type="text" name="labor" size="5" maxlength="5" value="<?php if (isset($_POST['labor'])) echo $_POST['labor']; ?>" /></p>
<fieldset><legend>Rates</legend>
<p>Professional Rate: <input type="radio" name="rate" size="5" maxlength="5" value="<?php if (isset($_POST['prorate'])) echo $_POST['prorate']; ?>" /></p>
<p>Regular Rate: <input type="radio" name="rate" size="5" maxlength="5" value="<?php if (isset($_POST['regrate'])) echo $_POST['regrate']; ?>" /></p></fieldset>
<fieldset><legend>Types of Paint</legend>
<p>Satin: <input type="radio" name="paint" size="5" maxlength="5" value="<?php if (isset($_POST['satin'])) echo $_POST['satin']; ?>" /></p>
<p>Flat: <input type="radio" name="paint" size="5" maxlength="5" value="<?php if (isset($_POST['flat'])) echo $_POST['flat']; ?>" /></p>
<p>Gloss/Semi Gloss: <input type="radio" name="paint" size="5" maxlength="5" value="<?php if (isset($_POST['gloss'])) echo $_POST['gloss']; ?>" /></p></fieldset>
<p><input type="submit" name="submit" value="Calculate!" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
6 replies to this topic
#1
Posted 18 January 2011 - 07:35 PM
I am trying to get output from my page. I want the person to be able to input in 3 of the fields and then click 1 radio button in 2 different fieldsets and then it calculate the total from all the prices and display but it won't calculate.
|
|
|
#2
Posted 18 January 2011 - 10:00 PM
You are trying to see if $_POST['ptotal'] exists, when it is never defined in your form to send that. You can always check what POST elements you have defined by placing this code at the top of your script:
I would also recommend you do not echo within the function calculate_total, rather return it and play with it in global scope. Just a suggestion.
print "<pre>"; print_r($_POST); print "</pre>";
I would also recommend you do not echo within the function calculate_total, rather return it and play with it in global scope. Just a suggestion.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 19 January 2011 - 05:03 AM
I did what you suggested as far as placing the code at the top of the script. This is all I get:
Array ( )
#4
Posted 19 January 2011 - 09:38 AM
Try using the form and filling out the boxes, then submitting, the code should display something (and you can see what is really sent)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 19 January 2011 - 02:36 PM
this is what is displayed but it should be doing math and displaying total costs which would include total square feet, paint and labor
[sqfeet] => 310 [pgallons] => 2 [labor] => 18 [prorate] => 17.50 [regrate] => [satin] => 31.50 [flat] => [gloss] => [submit] => Calculate! [submitted] => TRUE
#6
Posted 19 January 2011 - 06:09 PM
Thanks for everyones help. I ended up figuring it out. I ended up deleting everything and recoding it. Was so simple to figure out after I took a break and thought it out.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Module 2</title>
</head>
<body>
<?php #Script 3.5 - calculator.php
$page_title = 'Peters Professional Painters';
#Check for form submission:
if (isset($_POST['submitted'])) {
#Minimal form validation:
if ( is_numeric($_POST['painttotal']) &&
is_numeric($_POST['sqfeet']) &&
is_numeric($_POST['pgallons']) &&
is_numeric($_POST['rate']) &&
is_numeric($_POST['labortotal']) ) {
#Calculate results
$total = ($_POST['painttotal'] * $_POST['pgallons'] + $_POST['labortotal'] * $_POST['rate']);
#Print results
echo '<h1><center>Total Cost</center></h1>
<p><center>The total cost of painting ' . $_POST['sqfeet'] . ' square feet with ' . $_POST['pgallons'] . ' gallon(s) of paint at $' . $_POST['painttotal'] . ' per gallon and ' . $_POST['rate'] . '
hours of labor at $'. $_POST['labortotal'] . ' is $' . number_format ($total, 2) . '.</center></p><br />';
}
}
//Leave the PHP section and create the HTML form:
?>
<h1>Peter's Professional Painters Calculator</h1>
<form action="index.php" method="post">
<p>Disclaimer: For every 310 square feet of wall it will require 1 gallon of paint and 8 hours of labor</p><br />
<p>How many square feet?: <input type="text" name="sqfeet" size="5" maxlength="5" /></p>
<p>How many gallons of paint?: <input type="text" name="pgallons" size="5" maxlength="5" /></p>
<p>How many hours of labor?: <input type="text" name="rate" size="5" maxlength="5" /></p>
<p>Paint total: <input type="text" name="painttotal" size="5" maxlength="5" /><br />Satin = $31.50 , Flat = $24.00 , Gloss/Semi Gloss = $27.75</p>
<p>Rate total: <input type="text" name="labortotal" size="5" maxlength="5" /><br />Professional = $17.50/hour , Regular = $12.50/hour</p>
<p><input type="submit" name="submit" value="Calculate!" /></p>
<input type="hidden" name="submitted" value="1" />
</form>
</body>
</html>
#7
Posted 19 January 2011 - 11:56 PM
That looks a lot better, you just didn't really require a function up there, it broke the scope on most of your formulations and that is why it didn't work. I am glad you could fix it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









