Jump to content
Larry Ullman's Book Forums

Re: Script 3.5 On Chapter 3 Page 85


Recommended Posts

<?php # Script 3.5 calculator.php

 

$page_title = 'widget cost calculator';

include('includes/header.html');

// Check for form submission

 

if(isset($_POST['submitted'])) {

 

//Minimal form validation

 

if ( is_numeric($_POST['quantity'])&&

is_numeric($_POST['price'])&&

is_numeric($_POST['tax']) ){

 

//calculte the results

 

$total = ($_POST['quantity']*

$_POST['price']);

 

$taxrate = ($_POST['tax'] / 100); //turn 5% into .05.

 

$total += ($total * $taxrate); // Add tax

 

//Print the results

echo '<h1>Total cost</h1>

 

<p>The total cost of purchasing '. $_POST['quantity'] .' widget(s) at

$'.number_format ($_POST['price'], 2).' each, including a tax rate

of'. $_POST['tax'] .'%, is $'.number_format ($total, 2).'.</p>';

 

} else { // Invalid submited values.

 

echo '<h1>Error!</h1>

 

<p class="error">Please enter a valid

quantity, price, and tax.</p>';

 

}

 

} // End of main isset() If.

// Leave the php section and create the html form

 

?>

 

<h1>Widget cost calculator</h1>

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

<p>Quantity:

<input type="text" name="quantity" size="5" maxlength="5" />

</p>

<p>Price:

<input type="text" name="Price" size="5" maxlength="10" />

</p>

<p>Tax (%):

<input type="text" name="tax" size="5" maxlength="5" />

</p>

<p>

<input type="submit" name="submit" value="calculate" />

</p>

<input type="hidden" name="Submited" value="1" />

</form>

<?php //Include footer

include ('includes/footer.html');

?>

Link to comment
Share on other sites

Hi I am new to php and am stuck on script 3.5

 

The html form and the includes both work. When i fill out the form and press calculate it clears the form and echo's nothing back

 

I spent hour's last night trying to see where i have gone wrong

 

Im using wamp 2.1 which had php 5.3.5 and ive now used an add on 6.0 as i thought it may be that

Thanks Chris

Link to comment
Share on other sites

You need to use the value attribute of the input elements to echo ("sticky") values.

 

For example:

 

<input type="text" name="quantity" size="5" maxlength="5" value="<?php if (!empty($_POST['quantity'])) echo $_POST['quantity']; ?>" />
</p>

 

That is just one example, and the syntax may differ, depending on your situation. Actually, I'm not 100% sure my syntax is correct, and can't reference the book at the moment, but that's the general idea.

  • Upvote 1
Link to comment
Share on other sites

Thanks for the help,it worked. I used a capital s for submitted and i missed out a t too.

 

It now returns an error message(please enter a valid quantity, price and tax.) after filling out the form and will not calculate.

Link to comment
Share on other sites

Well, if you think about your code logically, then that means that it's failing the next if conditional. Try echoing the three posted values to the screen before you hit the if conditional and actually confirm whether you're getting the intended values (mainly, the values are actual numbers).

  • Upvote 1
Link to comment
Share on other sites

Hi agaain, ive found the problem. I used a capital P in price on the form.

 

It all now works as it should do.

 

Thanks for the help.

 

I spent hours trying to work out the mistake, i guess walking away helps too.

Link to comment
Share on other sites

Yeah, walking away does help. I have been stuck on problems for days, and then when I come back to it with a clear head, I instantly spot the problem. Happens to all programmers. Glad you solved your problem though.

 

I kinda wish it was one of those "once bitten, twice shy" sort of things, but I seem to have to make the same mistake many a-time before I learn my lesson.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...