Php variables not displaying data

hi,
i’m new to php however, when i output the contents of a variable using echo “”.$variablename; it displays zero in the result. Have changed this to $_POST[“variablename”]; however, still displays a zero. I know that there is data in the calculation of the variable but not sure as to why it displays the contents as 0. Have apache, php and mysql correctly installed. Phpinfo displays correctly with all the variables. register_globals variable is set to off. however, this should not be an issue. Am running php version 5.2.4. Below is the program code:

echo “

Your order is as follows:”; //F is yr in 4 digit format.
echo “
”;
echo $_POST[“tireqty”]." tires
“; // These variables display the data contents correctly.
echo $_POST[“oilqty”].” bottles of oil
“;
echo $_POST[“sparkqty”].” spark plugs
“;
$totalqty =0;
$totalamount=0.00;
$totalqty = $tireqty + $oilqty + $sparkqty;
define(“TIREPRICE”,100);
define(“OILPRICE”,10);
define(“SPARKPRICE”,4);
$totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;
$totalamount = number_format($totalamount, 2);
echo “Items Ordered: “.$totalqty.”
\n”; //----------> The variable $totalqty is showing as 0
echo “Subtotal: $”.$totalamount.”
\n"; //------> The variable $totalamount is showing as $0.00
$taxrate = 0.10; //local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
$totalamount = number_format($totalamount, 2);
echo “Total including Tax: $” .$totalamount. “
\n”; //-------->The variable $totalamount is showing as $0.00

output of program shows:
Order processed at 18:40, 27th September

Your order is as follows:
2 tires
4 bottles of oil
3 spark plugs
Items Ordered: 0
Subtotal: $0.00
Total including Tax: $0.00

You need to establish your $tireqty, $oilqty, and $sparkqty variables.

Try putting this at the top of your script:[php]$tireqty = $_POST[‘tireqty’];
$oilqty = $_POST[‘oilqty’];
$sparkqty = $_POST[‘sparkqty’];
[/php]

This should assign the expected values to them.

One thing to be aware of when working with forms:

You generally want to always check to see if the form was used to access the page.

Assuming your submit button is named “submit”, this would look like this with your code:[php]if(isset($_POST[‘submit’]))
{
$tireqty = $_POST[‘tireqty’];
$oilqty = $_POST[‘oilqty’];
$sparkqty = $_POST[‘sparkqty’];
echo “

Your order is as follows:”; //F is yr in 4 digit format.
echo “
”;
echo $_POST[“tireqty”]." tires
“; // These variables display the data contents correctly.
echo $_POST[“oilqty”].” bottles of oil
“;
echo $_POST[“sparkqty”].” spark plugs
“;
$totalqty =0;
$totalamount=0.00;
$totalqty = $tireqty + $oilqty + $sparkqty;
define(“TIREPRICE”,100);
define(“OILPRICE”,10);
define(“SPARKPRICE”,4);
$totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;
$totalamount = number_format($totalamount, 2);
echo “Items Ordered: “.$totalqty.”
\n”; //----------> The variable $totalqty is showing as 0
echo “Subtotal: $”.$totalamount.”
\n"; //------> The variable $totalamount is showing as $0.00
$taxrate = 0.10; //local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
$totalamount = number_format($totalamount, 2);
echo “Total including Tax: $” .$totalamount. “
\n”; //-------->The variable $totalamount is showing as $0.00
}
else echo ‘Sorry, you must use the correct form to access this page.’;[/php]

Note that php is case sensitive, so if your submit button is named ‘Submit’ instead of ‘submit’ you would need to change the isset check to reflect that.

awesome. thanks. putting the $_POST before the variable resolved the issue.

Glad to hear it Charles!

Jay

Sponsor our Newsletter | Privacy Policy | Terms of Service