Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am trying to do your tutorials and getting an HTTP 500 error. It is coming from this script. it is showing me errors in the php block (not the HTML) in Dreamweaver. It is coming from the isset() and the else lines. Do you see an error? The html will work without it. It is currently part of another page, and the rest of it is fine. I have tried to type it as it was shown in the book, although I tried a few other versions, but still got the same result. Should this use  $_GET[] instead?

<?php // 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; //65 mph
    //Print results
    echo '<div class="error"><h2>Total estimated cost</h2></div>
    <p>The total cost of driving is ' .$_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 an average of 65 mph, the trip will take approximately '. number_format($hours, 2). 'hours.</p>';}
        else { //invalid submitted values.
        echo '<div class="error"><h2>Error</h2><p>Please enter a valid distance, price per gallon, and fuel efficiency.</p></div>';

} //leave PHP section, create HTML form below.
 <section class="calculator">
     <h2>Trip Cost Calculator</h2>
     <form action="calculator1.php" method="post">
     <p>Distance (in miles). <input type="number" name="distance" placeholder="10"></p> <!-- add required to make it not submit  unless it is filled out -->
   <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>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>
         <p><input type="submit" name="submit" value="Calculate!"></p>

Link to comment
Share on other sites

I changed the isset line and removed the commas, replaced them with && and included the whole isset() block in a paraenthesis set,. I removed the placeholder code in the html form. The page now loads, but it is now only displaying the else clause, so it's still not working correctly.

Link to comment
Share on other sites


I think you're missing a set of parenthesis in your form validation. In the code you copied, you have:

  if (isset $_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency'])

But it should be as follows:

if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) &&
	 is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) {

Notice after the isset()  you are missing the opening parenthesis (before $_POST['distance'], but you have the closing one after $_POST['efficiency']. Your also missing the final closing parenthesis after the '&& is_numeric($_POST['efficiency'])' -- there should be a second parenthesis. The parenthesis before the  isset() and at the very end should essentially be 'wrapping itself' around all six of the validators (ie everything you're checking is TRUE via your IF statement).


I also noticed in your html inputs, it you have this code:

<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>

HTML values, I believe, always need to be assigned with the equals (=) operator, so it should be like this:

<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>

If you're getting this error: echo 'Please enter a valid distance, price per gallon, and fuel efficiency' it could be because the HTML inputs are not able to interpret the values of the radio options.

Another cause could be because you have inconsistent naming in your doc. In this line:

    $dollars = $gallons * $_POST['gallon-price'];

you name $_POST['gallon-price']; but in your HTML, you have it as 'gallon_price' -- which matches your PHP validation. Again, the HTML and PHP probably aren't able to communicate values when the naming is inconsistent or has an error. You should change that array value to 'gallon_price', so it looks like so:

$dollars = $gallons * $_POST['gallon_price'];

Lastly, and this may be nothing, in your form action, you have:

<form action="calculator1.php" method="post">

is that the name of the file on your server?

One other note, I noticed you said you use Dreamweaver -- I'd highly recommend downloading a text editor like Visual Studio Code, which will let you compare your code to Larry's code on the book's github. I haven't heard of anyone using DW in a the last several years, and VS Code is a free download with lots of plugins and extensions. It will highlight discrepancies which makes debugging and looking for syntactical errors much faster. I'd also recommend downloading and installing MAMP/LAMP and running your server via that application as well. Hopefully you're able to make the changes and get moving foward!


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...