Jump to content
Larry Ullman's Book Forums

Problem Getting Calculator.Php Form Ch3 To Work


Recommended Posts

Hi,

 

I'm going through Chapter 3, working on calculator.php and I'm not able to get the page to work with the simple form validation. I can get the calculator to work only be commenting out the validation check. I've stared at this for too long and I can't see where the problem is, I'd appreciate a second set of eyes to just look over what I've got and maybe point me in the right direction.

 

When I take the commenting off of the form validation I get this error:

 

Parse error: syntax error, unexpected $end in /home/scottm/public_html/tuts/php/calculator.php on line 57

 

I know the problem is somewhere in my validation because with it commented out the calculator works, but when I look at the uncommented validation code I just don't see where the problem is. Thanks in advance for your time.

 

<?php # Script 3.5 - calculator.php
// Error handling
//ini_set ('display_errors',1);//Let me learn from my mistakes!
//error_reporting (E_ALL | E_STRICT);// show me all errors
$page_title = 'Trip Cost Calculator';
include('includes/header.html');
//Write the conditional that checks for the form submission by checking the method (whether it's GET or POST)
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 '<h1>Estimated Total Cost</h1>
  <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 mph, the trip will take approximately' . number_format($hours, 2) . ' hours.</p>';

} else {//invalid submit values

 echo'<h1>Error!</h1>
 <p>Please submit valid distance, price per gallon and feul efficiency</p>';

} //end of main submission "if" statement
//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>Average 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'); ?>

Link to comment
Share on other sites

This error is often associated with missing braces. Do you have a text editor that can help you balance them? I know Notepad++ does for windows, that's free to download too. Anyway I think the problem at a quick glance is your missing '}' after the last echo statement.


} else { //invalid submit values
echo '<h1>Error!</h1>
 <p>Please submit valid distance, price per gallon and feul efficiency</p>';
 } // Missing brace
} //end of main submission "if" statement
//Leave the PHP section and create the html form
?>

Link to comment
Share on other sites

That was it!

 

I knew it was something simple, but for the life f me I was not able to see it. Thank you so much Jonathon, you have no idea how long I spent staring at that code. I'm currently working with Dreamweaver CS4, but I have been using Komodo Edit as well to have access to its syntax highlighting (DW is awful for php). Thanks again I really appreciate the second set of eyes on this script.

Link to comment
Share on other sites

  • 1 year later...

I am having problems with the same script. This is the error that is making me get the error message on my form: I am blue in the face and getting so frustrated as I just can't see what the problem is.  Notice: Undefined variable: POST in C:\Users\Owner\Sites\acquia-drupal\CO241\calculator.php on line 12    I keep getting the error on last line of IF statement  && is_numeric($POST['efficiency']) ) {

Any ideas appreciated.

 

<?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 '<h1>Total Estimated Costs</h1>
  <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 values.
    echo '<h1> Error!</h1>
    <p class="error">Please enter a valid distance, price per gallon, and fuel efficiency.</p>';
  }
 } //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'); ?>
     
            
            
            
            
  
  
  

Link to comment
Share on other sites

HARTLEYSAN

OMG  Thank you. How is it that after a while the more you look at your code, the less you see? I printed the code out, compared it with the text.. and did not see a thing!

Any ideas on how to get around this?

 

THANK YOU!

THANK YOU!

 THANK YOU!

My day is 100 percent better now.

Jaloney

Link to comment
Share on other sites

The more you practice the more you'll understand how to debug. In this case, the error message pointed you to a particular line - a large percentage of syntax errors are caused by an error in the preceding line, so be sure to scrutinise the error line and the previous one. As you code more, you'll probably realise that you make some errors consistently - I used to always forget the parentheses around an if clause, so I knew to look for that straight away.

 

A good text editor will help by colour coding different languages and highlighting where there's an error - I use TextWrangler on mac, its free and am currently trialling Coda2 and will also try Sublime Text as I've heard both are very good. Do a search and you'll find lots of options - I think they're may be a thread somewhere on this forum.

  • Upvote 1
Link to comment
Share on other sites

Well said, margaux.

As he said, a big part is just experience, and having made the same kind of error (often too many times to count).

Also, a fresh pair of eyes always helps.

Lastly, I'm a big fan of rubber duck debugging (http://en.m.wikipedia.org/wiki/Rubber_duck_debugging).

 

Beyond that, I will often copy and paste an error into Google and see if there is a common solution (or conversely, confirm that I'm simply being an idiot and need to look closer at my code one more time).

Link to comment
Share on other sites

 Share

×
×
  • Create New...