Getting index errors even though script works...why?
Posted: Sat Mar 13, 2010 4:56 pm
Greetings,
I'm a PHP n00b and am writing the following script for class. That being said, I've gotten 99% of the function out of the script that I want, but keep getting the following errors when accessing the page:
Notice: Undefined index: number1 in beer.php on line 6
Notice: Undefined index: number2 in beer.php on line 7
Notice: Undefined index: number3 in beer.php on line 8
which I can clear up by including:
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
but I'd like to know a)why they appear even if the script works and b)how can I get rid of them legitimately.
My script is below; any help you can offer (the simpler the better!) would be greatly appreciated.
<?php
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
$a = $_POST["number1"];
$b = $_POST ["number2"];
$c = $_POST["number3"];
if (!isset($_POST['submit'])){
?>
<html>
<head>
<title>Beer $tore</title>
</head>
<body>
<div>
<h2>Welcome. Order a few drinks!</h2></div>
<div> <u>All prices are in Ameros</u> </div> <br />
<form action="beer.php" method="post">
<b>Arrogant Bastard</b> by Stone Brewing Co. <b>8.75/ea.</b><br>
How many? <input type = "text" name="number1" type="text" size="2" /> <p>
<b>Storm King</b> by Victory Brewing Co. <b>9.25/ea.</b><br>
How many? <input type ="text" name="number2" type="text" size="2" /> <p>
<b>Skull Splitter</b> by Orkney Brewery <b>11.50/ea.</b> <br>
How many? <input type ="text" name="number3" type="text" size="2" /> <p>
<input type="submit" name="submit" value="Calculate your total" />
</form>
<div> *Tax rate of 6.35% applies to all orders. <br /><br /></div>
<div><b>**If the total number of beers you order matches the random number of the moment, your order is free! Good luck!!</b></div>
</body></html>
<?php
} else {
$beer_num = $a + $b + $c;
$cost = ($a * 8.75) + ($b * 9.25) + ($c * 11.50);
$tax = .0635;
$tax = $tax + 1;
$total = $cost * $tax;
$total = number_format ($total, 2);
print '<p><h2><u>Checkout Confirmation</u></h2></p>';
print "<p>You have ordered <b> $beer_num </b> beers for a total cost of $total Ameros. <b>Pay up</b>.</p>";
$n1 = rand (1, 25);
print "<p>The random number of the moment is <b>$n1</b>.</p>";
if ($n1 == $beer_num) {
print '<p><b>YOU WON! Enjoy your free beer.</b></p>';
} else {
print '<p><b>No</b> free beer for you. Better luck next time!</p>';
}
}
?>
</body></html>
Thanks for your help and your time!
I'm a PHP n00b and am writing the following script for class. That being said, I've gotten 99% of the function out of the script that I want, but keep getting the following errors when accessing the page:
Notice: Undefined index: number1 in beer.php on line 6
Notice: Undefined index: number2 in beer.php on line 7
Notice: Undefined index: number3 in beer.php on line 8
which I can clear up by including:
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
but I'd like to know a)why they appear even if the script works and b)how can I get rid of them legitimately.
My script is below; any help you can offer (the simpler the better!) would be greatly appreciated.
<?php
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
$a = $_POST["number1"];
$b = $_POST ["number2"];
$c = $_POST["number3"];
if (!isset($_POST['submit'])){
?>
<html>
<head>
<title>Beer $tore</title>
</head>
<body>
<div>
<h2>Welcome. Order a few drinks!</h2></div>
<div> <u>All prices are in Ameros</u> </div> <br />
<form action="beer.php" method="post">
<b>Arrogant Bastard</b> by Stone Brewing Co. <b>8.75/ea.</b><br>
How many? <input type = "text" name="number1" type="text" size="2" /> <p>
<b>Storm King</b> by Victory Brewing Co. <b>9.25/ea.</b><br>
How many? <input type ="text" name="number2" type="text" size="2" /> <p>
<b>Skull Splitter</b> by Orkney Brewery <b>11.50/ea.</b> <br>
How many? <input type ="text" name="number3" type="text" size="2" /> <p>
<input type="submit" name="submit" value="Calculate your total" />
</form>
<div> *Tax rate of 6.35% applies to all orders. <br /><br /></div>
<div><b>**If the total number of beers you order matches the random number of the moment, your order is free! Good luck!!</b></div>
</body></html>
<?php
} else {
$beer_num = $a + $b + $c;
$cost = ($a * 8.75) + ($b * 9.25) + ($c * 11.50);
$tax = .0635;
$tax = $tax + 1;
$total = $cost * $tax;
$total = number_format ($total, 2);
print '<p><h2><u>Checkout Confirmation</u></h2></p>';
print "<p>You have ordered <b> $beer_num </b> beers for a total cost of $total Ameros. <b>Pay up</b>.</p>";
$n1 = rand (1, 25);
print "<p>The random number of the moment is <b>$n1</b>.</p>";
if ($n1 == $beer_num) {
print '<p><b>YOU WON! Enjoy your free beer.</b></p>';
} else {
print '<p><b>No</b> free beer for you. Better luck next time!</p>';
}
}
?>
</body></html>
Thanks for your help and your time!