Jump to content
Larry Ullman's Book Forums

Issues with Script 3.5 - calculator.php


Recommended Posts

Hello All!  I'm hoping to get some help with my validation check on this exercise.  Basically what's happening is the calculator will work, but only if I comment out the 'minimal form validation' php script.  I read in an earlier post that it might have something to do w/ missing braces, but I don't think that's the case as I've looked at this code both in VS Code and Brackets and both show the braces as being balanced.  I also don't get any errors when debugging or saving the .php file. 

I have a hunch it might have something to do w/ the radio options in the HTML portion, or the value of the fuel efficiency in the html, but I'm not sure, as the calculation is performed when I comment out the validation, so perhaps I have a mistake in that syntax.  I've been looking at this on and off for the last 2 days and found/fixed some other errors, but I can't seem to find this one.  I appreciate any help, and please don't feel you have to give me the answer outright, just a nudge in the right direction is all I am asking.  Thanks Again!

 

My Code:

 

<?php #Script 3.5 - calculator.php


$page_title = 'Trip Cost Calculator';
include('/Applications/MAMP/htdocs/Chapter 3 Files/includes/header.html');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
     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;


    //printing results
    echo '<div class="page-header"><h1>Total Estimated Cost</h1></div>
    <p>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.</p>';
        
    }  else { //Invalid submitted forms
        echo '<div class="page-header"><h1>Error!</h1></div>
        <p class ="text-danger">Please enter a valid distance, price per gallon, and fuel efficiency.</p>';
       }
}

//leaving PHP and now making HTML form
?>

<div class="page-header"><h1>Trip Cost Calculator</h1></div>
<form action="calculator.php" method ="post">

    <p>Distance (in miles): <input type="number" name = "distance"></p>
    <p>Avg. Price Per Gallon: <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</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('/Applications/MAMP/htdocs/Chapter 3 Files/includes/footer.html'); ?>
 

 

Link to comment
Share on other sites

Hey Carbs,

I appreciate the heads up on the white space, and tightened it up, but didn't see a difference.  When I was doing that though, I looked at the whole file w/ 'fresh eyes,' and caught that I had missed an underscore in the validation check:

&& is_numeric($_POST['gallon price']) && -- there should be an underscore between gallon and price.

I added the underscore and it worked perfectly.  Thank you for your time looking this over for me!  Crazy how a little slip up like that got me and bottlenecked my progress, but it's a mistake I'll likely not make as much in the future.

Thanks Again and have a great week!

Link to comment
Share on other sites

 Share

×
×
  • Create New...