Jump to content
Larry Ullman's Book Forums

I'm Having Trouble In Chapter 3 With Script 3.6... Help Plz...


Recommended Posts

This is the script that I'm having trouble with: 

<?php # Script 3.6 - calc3_2.php 

// This sets the page's title:
$page_title = 'Trip Cost Calculator';

// This includes the header:
include ('includes/header.html');

// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	// Minimal form validation... Checks if distance, gallon_price, & efficiency are set & numeric...
	
	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']; // This divides the distance the user enters by the efficiency the users enters & assigns it to the variable $gallons...
	$dollars = $gallons * $_POST['gallon_price']; // This multiplies the gallons by the gallon price the user selected & assigns it to $dollars...
	$hours = $_POST['distance']/65; // This divides the distance the user enters by 65... 
	
	// Print the results:
	echo '<h1>Total Estimated 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 miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.</p>';
	
} else { // If the user did something wrong
	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 & create the HTML form:

?>

<h1>Trip Cost Calculator</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
	<p>Distance (in miles): <input type="text" name="distance" value="<?php if (isset($_POST['distance'])) echo $_POST['distance']; ?>" /></p>
	<p>Ave. Price Per Gallon: <span class="input">
		 
		<input type="radio" name="gallon_price" value="3.00" <?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.00')) echo 'checked="checked" '; ?>/> 3.00 
		
		<input type="radio" name="gallon_price" value="3.50"
		<?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.50')) echo 'checked="checked" '; ?>/> 3.50
		
		<input type="radio" name="gallon_price" value="4.00" <?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '4.00')) echo 'checked="checked" '; ?>/> 4.00
	</span><p>
	<p>Fuel Efficiency: <select name="efficiency">
		
		<option value="10" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '10')) echo ' selected="selected"'; ?>>Terrible</option>
		
		<option value="20" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '20')) echo ' selected="selected"'; ?>>Decent</option>
		
		<option value="30" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo ' selected="selected"'; ?>>Very Good</option>
		
		<option value="50" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '50')) echo ' selected="selected"'; ?>>Outstanding</option>
		
	</select></p>
	
	<p><input type="submit" name="submit" value="Calculate!" /></p>
	
</form>

<?php include ('includes/footer.html'); ?>

I understand everything until this part: 


<h1>Trip Cost Calculator</h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
	<p>Distance (in miles): <input type="text" name="distance" value="<?php if (isset($_POST['distance'])) echo $_POST['distance']; ?>" /></p>
	<p>Ave. Price Per Gallon: <span class="input">
		 
		<input type="radio" name="gallon_price" value="3.00" <?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.00')) echo 'checked="checked" '; ?>/> 3.00 
		
		<input type="radio" name="gallon_price" value="3.50"
		<?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.50')) echo 'checked="checked" '; ?>/> 3.50
		
		<input type="radio" name="gallon_price" value="4.00" <?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '4.00')) echo 'checked="checked" '; ?>/> 4.00
	</span><p>
	<p>Fuel Efficiency: <select name="efficiency">
		
		<option value="10" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '10')) echo ' selected="selected"'; ?>>Terrible</option>
		
		<option value="20" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '20')) echo ' selected="selected"'; ?>>Decent</option>
		
		<option value="30" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo ' selected="selected"'; ?>>Very Good</option>
		
		<option value="50" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '50')) echo ' selected="selected"'; ?>>Outstanding</option>
		
	</select></p>
	
	<p><input type="submit" name="submit" value="Calculate!" /></p>
	
</form>
// I know the part below includes the footer
<?php include ('includes/footer.html'); ?>

I would very much appreciate it if you could break the script down for me & explain it for me. The parts like this are really confusing me - I don't understand what they do for the rest of the script: 

<option value="30" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo ' selected="selected"'; ?>>Very Good</option>
	
	
?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.50')) echo 'checked="checked" '; ?>/> 3.50
Link to comment
Share on other sites

Hello,

 

I would very much appreciate it if you could break the script down for me & explain it for me. The parts like this are really confusing me - I don't understand what they do for the rest of the script: 

<option value="30" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo ' selected="selected"'; ?>>Very Good</option>
	
	
?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.50')) echo 'checked="checked" '; ?>/> 3.50

 

These lines make the dropdown menu and the radio buttons sticky. For $_POST['efficiency'] to be set, the form must have been sent and the user must have chosen one option in the dropdown menu. So, if the form has already been sent but the user forgot something (either 'distance' or 'gallon_price') and the partially completed form is printed again...

if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30'))

... first checks that the user chose one option in the 'efficiency' dropdown menu (isset($_POST['efficiency']), and if the chosen value is '30' the PHP script will add the 'selected="selected"' attribute inside this option element. On screen, the 'Very Good' option of the dropdown menu will be preselected, so that the user doesn't have to select it again. And if you choose "View source" in your browser, you will see that the previously selected option shows the 'selected="selected"' attribute inside the option element:

<option value="30" selected="selected">Very Good</option>

If the user chose '20' in the dropdown menu, this option element will get the 'selected="selected"' attribute instead, and so on.

 

If the user forgot to choose one of the options in the dropdown menu, the default option (10/Terrible) will get the 'selected="selected"' attribute.

 

The radio buttons work in the same way. If the user didn't completely fill in the form, the radio button that had been chosen first time round will get the  'checked="checked"' attribute. On the other hand, if the user forgot to choose one of the radio buttons, none of them will get the 'checked="checked"' attribute.

 

If you want to better understand how the $_POST array works, you can print its contents just after the closing </form> tag:

</form>
<?php
echo '<p>Contents of POST <em>before</em> the form was submitted:</p>
<pre>' . print_r ($_POST, 1) . '</pre>';
?>

and again just after the line checking for form submission: 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo '<p>Contents of POST <em>after</em> the form was submitted:</p>
<pre>' . print_r ($_POST, 1) . '</pre>';

And test your script with data in 1, 2 or 3 form fields to see the consequences on the $_POST array.

 

 

 

 

I hope this helps,

 

Emilie

  • Upvote 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...