Newbee Calculator Post Form script doesn't work

This is my class assignment. Looks good but it doesn’t show error messages or calculate.

[php]

<?php # Script 3.5 - calculator.php $page_title = 'Trip Cost Calculator'; include ('includes/header.html'); // Check for form submission; if($_Server['REQUEST_METHOD'] == 'POST') { //Minimal form validation if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($POST['efficiency']) ) { //Calculate the results: $gallons = $_POST['distance'] / $_POST['efficiency']; $dollars = $gallons * $_POST['gallon_price']; $hours = $_POST['distance']/ 65; //Print the results: echo '

Total Estimated Costs

The total cost of driving ' . $_POST['distance'] . ' miles, averaging ' . $_POST ['efficiency'] . ' miles per gallon, and paying an average of $' . $_POST['gallon_price'] . ' per gallon, is $' . number_format($dollars, 2) . '. If you drive at an average of 65 miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.

'; } else { //Invalid submitted values. echo '

Error!

Please enter a valid distance, price per gallon, and fuel efficiency.

'; } } //End of main submission IF. //Leave the PHP section and create the HTML form: ?>
<h1>Trip Cost Calculator</h1>
<form action="calculator.php" method="post">
<p>Distance (in miles): <input type="text" name="distance" /></p>
<p>Ave. Price Per Gallon: <span class="input">
    <input type="radio" name="gallon_price" value="3.00" /> 3.00
    <input type="radio" name="gallon_price" value="3.50" /> 3.50
    <input type="radio" name="gallon_price" value="4.00" /> 4.00
    </span></p>
    <p>Fuel Efficiency: <select name="efficiency">
         <option value="10">Terrible</option>
         <option value="20">Decent</option>  
         <option value="30">Very Good</option>  
         <option value="50">Outstanding</option>
         </select></p>
         <p><input type="submit" name="submit" value="Calculate!" /></p>
         </form>
         
         <?php include ('includes/footer.html'); ?>
  
         [/php]

Okay I finally figured out that SERVER in the first line of form validation script wasn’t in all capitals. Now I am down to one error that is in last line of form validation IF script below (&& is_numeric($POST[‘efficiency’]) ) {)
As efficiency is not numeric but its value is numeric… I don’t know if this is the problem?
[php]

<?php # Script 3.5 - calculator.php $page_title = 'Trip Cost Calculator'; include ('includes/header.html'); // Check for form submission; if($_SERVER['REQUEST_METHOD'] == 'POST') { //Minimal form validation if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($POST['efficiency']) ) { //Calculate the results: $gallons = $_POST['distance'] / $_POST['efficiency']; $dollars = $gallons * $_POST['gallon_price']; $hours = $_POST['distance']/ 65; //Print the results: echo '

Total Estimated Costs

The total cost of driving ' . $_POST['distance'] . ' miles, averaging ' . $_POST ['efficiency'] . ' miles per gallon, and paying an average of $' . $_POST['gallon_price'] . ' per gallon, is $' . number_format($dollars, 2) . '. If you drive at an average of 65 miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.

'; } else { //Invalid submitted values. echo '

Error!

Please enter a valid distance, price per gallon, and fuel efficiency.

'; } } //End of main submission IF. //Leave the PHP section and create the HTML form: ?>
<h1>Trip Cost Calculator</h1>
<form action="calculator.php" method="post">
<p>Distance (in miles): <input type="text" name="distance" /></p>
<p>Ave. Price Per Gallon: <span class="input">
    <input type="radio" name="gallon_price" value="3.00" /> 3.00
    <input type="radio" name="gallon_price" value="3.50" /> 3.50
    <input type="radio" name="gallon_price" value="4.00" /> 4.00
    </span></p>
    <p>Fuel Efficiency: <select name="efficiency">
         <option value="10">Terrible</option>
         <option value="20">Decent</option>  
         <option value="30">Very Good</option>  
         <option value="50">Outstanding</option>
         </select></p>
         <p><input type="submit" name="submit" value="Calculate!" /></p>
         </form>
         
         <?php include ('includes/footer.html'); ?>
  
         
         
         
         
	
	
	  [/php]

Since you are a newbie, you should avoid picking up bad habits early on and learn to properly format your code. Here is the standards that I follow personally.

http://pear.php.net/manual/en/standards.php

You actually posted the problem already. It is here:

[php]is_numeric($POST[‘efficiency’])[/php]

It should be $_POST not $POST

THANK YOU MATT.
Yes my biggest problem is not seeing my errors… even when I know what line they are on. I will check out your link.
Thank you!
Jaloney

Sponsor our Newsletter | Privacy Policy | Terms of Service