amavadia Posted October 22, 2009 Share Posted October 22, 2009 Hi, I've just started to learn php today from a book, and ive created this example for a calculator but i keep getting an error message saying there is an unidentified variable. There is a html page with the forms on it: <HTML> <HRAD> <TITLE>Caluclation Form</TITLE> </HEAD> <BODY> <FORM METHOD="POST" ACTION="calculate.php"> <p>Value 1: <INPUT TYPE="text" NAME="val1" SIZE=10></p> <p>Value 2: <INPUT TYPE="text" NAME="val2" SIZE=10></p> <p>Calculation:<br> <INPUT TYPE="radio" NAME="calc" VALUE="add"> add<br> <INPUT TYPE="radio" NAME="calc" VALUE="subtract"> subtract<br> <INPUT TYPE="radio" NAME="calc" VALUE="multiply"> multiply<br> <INPUT TYPE="radio" NAME="calc" VALUE="divide"> divide</p> <p><INPUT TYPE="submit" NAME="submit" VALUE="Calculate"></p> </FORM> </BODY> </HTML> and the php script in a nother file: <php if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) { header("Location: http://127.0.0.1/calculate_form.html"); } if ($_POST[calc] == "add") { $result = $_POST[val1] + $_POST[val2]; } else if ($_POST[calc] == "subtract") { $result = $POST[val1]] - $_POST[val2]; } else if ($_POST[calc] == "multiply") { $result = $POST[val1]] * $_POST[val2]; } else if ($_POST[calc] == "divide") { $result = $POST[val1]] / $_POST[val2]; } ?> <HTML> <HEAD> <TITLE>Calculation Result</TITLE> </HEAD> <BODY> <p>The result of the calculation is: <?php echo "$result"; ?></p> </BODY> </HTML> When you don't enter anything into the calc, it is meant to reload the page, but i get this: The result of the calculation is: Notice: Undefined variable: result in C:\wamp\www\calculate.php on line 28 Can anyone see what the problem is? Probably something really obvious but im stumped :-\ Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/178635-newbie-needs-help/ Share on other sites More sharing options...
KevinM1 Posted October 22, 2009 Share Posted October 22, 2009 To get a hold of a form's values, the input name needs to be in quotes. Example: $firstValue = $_POST['val1']; Also, you have a couple of typos where you have two ending brackets (]]) at the end of your post values. Quote Link to comment https://forums.phpfreaks.com/topic/178635-newbie-needs-help/#findComment-942243 Share on other sites More sharing options...
garethhall Posted October 22, 2009 Share Posted October 22, 2009 You also have other options that might be better to use, but it real depends on what you are trying to do. You also don't need the extra brackets "()". This <?php if ($_POST['val1'] == "" || $_POST['val2'] == "" || $_POST['calc'] == "") { header("Location: http://127.0.0.1/calculate_form.html"); } ?> Or This <?php if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['calc'])) { header("Location: http://127.0.0.1/calculate_form.html"); } ?> Or This <?php if (!isset($_POST['val1']) || !isset($_POST['val2']) || !isset($_POST['calc'])) { header("Location: http://127.0.0.1/calculate_form.html"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178635-newbie-needs-help/#findComment-942324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.