Tax Calculator Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seral1969
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2008 5:56 pm

Tax Calculator Problem

Post by seral1969 »

Hi,

I'm just starting PHP, and have an old textbook with a simple tax calculator problem...

I have typed the code in as instructed, but cannot get the desired result...

I believe the book's example does not include varible declarations...

I have no idea what the SecondTime variable is for...

PLEASE HELP!!!


Here is my code...
-----------------------------------------------------------------------------------------------------------------------
<html>
<head>

<title> Sales Tax Calculator </title>
</head>

<?
if ($SecondTime == "")
{
echo "
<body><h1> Please enter amount and tax rate </h1>
<br>
<br>
<form action = \"taxcalc.php\" method = \"post\">
<input type = \"hidden\" name = \"SecondTime\" Value = 1>
Amount &nbsp;
<input type = text name\"amount\" size = 15><br><br>
Tax Rate: &nbsp;
<Input type = text name = \"rate\" size = 15><br><br>
<Input type = submit name = \"submit\" value = \"Submit form\">
&nbsp; &nbsp;
<Input type = reset name = \"reset\" value = \"Clear form\"
";

} else
{


$calc_tax = $amount + ($amount * ($rate / 100));
$amount = number_format($amount,2,'.','');
$calc_tax = number_format($calc_tax,2,'.','');
echo "
<body><h1> The calculated tax rate is as follows: </h1>
<table><tr><td> Enter Amount: </td>
<td>$ $amount </td></tr>
<tr><td> Entered Tax Rate: </td>
<td> $rate % </td></tr>
<tr><td> Calculated Tax Amount: </td>
<td>$ $calc_tax </td></tr>
</table>
<br>
<form action = \"taxcalc.php\" method =\"post\">
<input type = \"hidden\" name = \"SecondTime\" Value = \"\">
<Input type = submit name = \"submit\" value = \"Return to Calculator\">
";
}
?>
</body>
</html>
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Re: Tax Calculator Problem

Post by jwalsh »

In the code you pasted the If Statement will always be true. The $SecondTime variable is never given a value.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Tax Calculator Problem

Post by califdon »

Your script is missing any code to determine the values of any of the Form variables, passed in the $_POST superglobal variable array. These variables must be given values before using them. You do this by going through the $_POST associative array, like this:

Code: Select all

$amount=$_POST['amount'];
$rate=$_POST['rate'];
$SecondTime=$_POST['SecondTime'];
When you have a Form with the action parameter set to call the same script (which your example is designed to do, assuming that you DID name the script "taxcalc.php"), your script has to know whether it is being called for the first time (in which case it should display the form) OR if it is being called as a result of the Form being submitted. There are several ways to determine which one is the case, each time. The example has a hidden field named "SecondTime" with a value of "1". When the script checks to find the value of the POSTed value for "SecondTime", if it is "1", it doesn't display the Form, it does the calculation instead. That's perhaps not the best way, but it works. I think it's more common to test whether the "Submit" variable is set, but there are various reasons why sometimes it's better to use one method or another.
seral1969
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2008 5:56 pm

Re: Tax Calculator Problem

Post by seral1969 »

That helped, but the amount and calculated interest does not display... just the tax rate...
seral1969
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2008 5:56 pm

Re: Tax Calculator Problem

Post by seral1969 »

It was a typo...
I fixed it...

It works!!!

You RULE!!!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Tax Calculator Problem

Post by califdon »

seral1969 wrote:That helped, but the amount and calculated interest does not display... just the tax rate...
Where did you put the code that assigns values to the $_POST variables? It should go before the first IF statement.

If it still does not display, check your code to make sure that all the Input element names match what you are looking for in assigning the variables. Remember, PHP is case sensitive.

As a side comment, you mentioned that your book is rather old, and I see that the example script does not conform to a lot of the current practices. Starting with the book is fine, and the code should still work, but you may want to spend some time with online references and tutorials, of which there are a lot of good ones. My all-time favorite is http://w3schools.com and the authoritative reference is http://www.php.net/docs.php.

I would probably write your script this way (note the differences):

Code: Select all

<html>
<head>
<title> Sales Tax Calculator </title>
</head>
<body>
 
<?php
if (isset($_POST['SecondTime']) {
   $SecondTime = $_POST['SecondTime'];
}
if (isset($_POST['amount']) {
   $amount= $_POST['amount'];
}
if (isset($_POST['rate']) {
   $rate= $_POST['rate'];
}
 
if ($SecondTime == "") {
   // Data is NOT being passed from the form, so display the form:
?>
   <h1> Please enter amount and tax rate </h1>
   <br />
   <br />
<?php
   echo "<form action = 'taxcalc.php' method = 'post'>";
   echo "<input type = 'hidden' name = 'SecondTime' Value = 1>Amount &nbsp;";
   echo "<input type = 'text' name = 'amount' size = 15><br /><br />Tax Rate: &nbsp;";
   echo "<Input type = 'text' name = 'rate' size = 15><br /><br />";
   echo "<Input type = 'submit' name = 'submit' value = 'Submit form'>&nbsp; &nbsp;";
   echo "<Input type = 'reset' name = 'reset' value = 'Clear form'";
   echo "</form>";
} else {
   // Data is being passed from the form, so calculate the tax:
   $calc_tax = $amount + ($amount * ($rate / 100));
   $amount = number_format($amount,2,'.','');
   $calc_tax = number_format($calc_tax,2,'.','');
?>
   <h1> The calculated tax rate is as follows: </h1>
   <table><tr><td> Enter Amount: </td>
      <td>$ $amount </td></tr>
      <tr><td> Entered Tax Rate: </td>
      <td><?php echo $rate ?> % </td></tr>
      <tr><td> Calculated Tax Amount: </td>
      <td>$<?php echo $calc_tax ?> </td></tr>
   </table>
   <br />
   <a href="taxcalc.php"><Input type = 'button' name value = 'Return to Calculator'></a>
   </form>
<?php
}
?>
</body>
</html>
 
 
While rewriting your script, I noticed the omission of an = sign in the Input syntax for the amount, which is why you weren't receiving a value for it.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Tax Calculator Problem

Post by califdon »

Ha! Just as I was ready to hit the Submit button on my reply, I heard a "Bing!" and it was you finding your typo! :D
Post Reply