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
<input type = text name\"amount\" size = 15><br><br>
Tax Rate:
<Input type = text name = \"rate\" size = 15><br><br>
<Input type = submit name = \"submit\" value = \"Submit form\">
<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>
Tax Calculator Problem
Moderator: General Moderators
Re: Tax Calculator Problem
In the code you pasted the If Statement will always be true. The $SecondTime variable is never given a value.
Re: Tax Calculator Problem
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:
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.
Code: Select all
$amount=$_POST['amount'];
$rate=$_POST['rate'];
$SecondTime=$_POST['SecondTime'];Re: Tax Calculator Problem
That helped, but the amount and calculated interest does not display... just the tax rate...
Re: Tax Calculator Problem
It was a typo...
I fixed it...
It works!!!
You RULE!!!
I fixed it...
It works!!!
You RULE!!!
Re: Tax Calculator Problem
Where did you put the code that assigns values to the $_POST variables? It should go before the first IF statement.seral1969 wrote:That helped, but the amount and calculated interest does not display... just the tax rate...
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 ";
echo "<input type = 'text' name = 'amount' size = 15><br /><br />Tax Rate: ";
echo "<Input type = 'text' name = 'rate' size = 15><br /><br />";
echo "<Input type = 'submit' name = 'submit' value = 'Submit form'> ";
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>
Re: Tax Calculator Problem
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! 