Wiernusz Posted January 16, 2009 Share Posted January 16, 2009 Hello. I am currently following a tutorial on building a simple calculator. Here is my calculation code: <?PHP if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) { header("Location: calculate_form.php"); exit; } 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]; } ?> And here is my form code: <html> <head> <title>Calculation Form</title> </head> <body> <font size="3" face="verdana"> <FORM METHOD="post" ACTION="calc.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"> <b>+</b> ] [<INPUT TYPE="radio" NAME="calc" VALUE="subtract"> - ] [<INPUT TYPE="radio" NAME="calc" VALUE="multiply"> <b>*</b> ] [<INPUT TYPE="radio" NAME="calc" VALUE="divide"> <b>/</b> ]</P> <P><INPUT TYPE="submit" NAME="submit" VALUE="Calculate"></P> </FORM> </body> </html> It apparently works, but it also gives me these warnings, along with the correct math answer; Notice: Use of undefined constant val1 - assumed 'val1' in E:\wamp\www\Learning\Calculator\calc.php on line 2 Notice: Use of undefined constant val2 - assumed 'val2' in E:\wamp\www\Learning\Calculator\calc.php on line 2 Notice: Use of undefined constant calc - assumed 'calc' in E:\wamp\www\Learning\Calculator\calc.php on line 2 Notice: Use of undefined constant calc - assumed 'calc' in E:\wamp\www\Learning\Calculator\calc.php on line 6 Notice: Use of undefined constant calc - assumed 'calc' in E:\wamp\www\Learning\Calculator\calc.php on line 8 Notice: Use of undefined constant val1 - assumed 'val1' in E:\wamp\www\Learning\Calculator\calc.php on line 9 Notice: Use of undefined constant val2 - assumed 'val2' in E:\wamp\www\Learning\Calculator\calc.php on line 9 I've put this together using a PHP 5 tutorial from a book, and have gone over it 4 times to make sure I havn't missed anything. Any help/tips would be greatly appreciated. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/ Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 <?php if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) { header("Location: calculate_form.php"); exit; } 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']; } ?> isset and using ' around array indexes should solve your woes. Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738632 Share on other sites More sharing options...
Mark Baker Posted January 16, 2009 Share Posted January 16, 2009 $_POST[val1] looks for a constant called val1, and would substitute in whatever value was set for that content as the key for the entry in the $_POST array But no constant of that name has been set, so it gives you a warning to say that it's going to treat val1 as a string key Use $_POST['val1'] Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738634 Share on other sites More sharing options...
rblake81 Posted January 16, 2009 Share Posted January 16, 2009 Hi Wiernusz, You need to check that the $_POST data are actually assigned values before you use them in that fashion. $value1 = isset($_POST['val1']) ? $_POST['val1'] : ""; $value2 = isset($_POST['val2']) ? $_POST['val2'] : ""; etc.... Then you can write your code: if((empty($value1)) || (empty($value2))) { etc.. } Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738636 Share on other sites More sharing options...
Wiernusz Posted January 16, 2009 Author Share Posted January 16, 2009 Thanks for all the quick replys guys. It's appreciated. I am starting to think this book I am following is out-dated or just plain wrong. Still trying to get it to work following all of your advice. Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738644 Share on other sites More sharing options...
Wiernusz Posted January 16, 2009 Author Share Posted January 16, 2009 Considering I am a PHP beginner, and this book is telling me to do things the wrong way (from the looks of it)... I am just going to scratch this whole thing, because I am just plain lost. One thing I would really like to know though, is the completed code for this to actually work so I can review it, and not have these past few hours of learning be a waste. If anyone is willing to show me the working script with no Notices/errors, I would be a happy person (for now!) Again, thanks for the help everybody. Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738654 Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 Ummm.... <?php if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) { header("Location: calculate_form.php"); exit; } 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']; } echo 'My calculations returned: ' . $result; ?> That would be the proper way to do it. Is that giving you an error/notice? Chances are the book is just plain wrong. Programmers are not very good book writers. Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738668 Share on other sites More sharing options...
Wiernusz Posted January 16, 2009 Author Share Posted January 16, 2009 Hey, thanks for posting the working script. I really appreciate it! :-) Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738673 Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 Still giving those errors. Thanks alot for the help, anyways. I'm just going to find another place to start learning. Well that may be, but I think you are saving the wrong file. The code I posted should not produce any notice errors. My 2cents, you are saving the wrong file. Here is the code I tested on my server, with no notice errors etc. <?php error_reporting(E_ALL); ini_set("display_errors", 1); if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) { header("Location: calculate_form.php"); exit; } if ($_POST['calc'] == "add") { $result = $_POST['val1'] + $_POST['val2']; $what = " plus "; } else if ($_POST['calc'] == "subtract") { $result = $_POST['val1'] - $_POST['val2']; $what = " minus "; } else if ($_POST['calc'] == "multiply") { $result = $_POST['val1'] * $_POST['val2']; $what = " times "; } else if ($_POST['calc'] == "divide") { $result = $_POST['val1'] / $_POST['val2']; $what = " divided by "; } echo 'My calculations returned: ' . $_POST['val1'] . $what . $_POST['val2'] . " = " . $result; ?> Which returned: My calculations returned: 2 plus 3 = 5 Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738684 Share on other sites More sharing options...
Wiernusz Posted January 16, 2009 Author Share Posted January 16, 2009 Still giving those errors. Thanks alot for the help, anyways. I'm just going to find another place to start learning. Well that may be, but I think you are saving the wrong file. The code I posted should not produce any notice errors. My 2cents, you are saving the wrong file. Here is the code I tested on my server, with no notice errors etc. <?php error_reporting(E_ALL); ini_set("display_errors", 1); if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) { header("Location: calculate_form.php"); exit; } if ($_POST['calc'] == "add") { $result = $_POST['val1'] + $_POST['val2']; $what = " plus "; } else if ($_POST['calc'] == "subtract") { $result = $_POST['val1'] - $_POST['val2']; $what = " minus "; } else if ($_POST['calc'] == "multiply") { $result = $_POST['val1'] * $_POST['val2']; $what = " times "; } else if ($_POST['calc'] == "divide") { $result = $_POST['val1'] / $_POST['val2']; $what = " divided by "; } echo 'My calculations returned: ' . $_POST['val1'] . $what . $_POST['val2'] . " = " . $result; ?> Which returned: My calculations returned: 2 plus 3 = 5 It seems as you were typing this, I edited my previous post... I did end up getting it working with your previous post. Thanks alot! It did work For the most part, I see what was wrong, and it's weird the publisher hadn't mentioned it. But to say the least - the book has been the best one yet (no throwing in stuff and not telling me about it!). Considering how much I've learned here compared to a whole bunch of other places, I decided I will finish this book. Helping me fix that code taught me a few things, and props to you for taking time to help me out here. You rock! Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-738733 Share on other sites More sharing options...
PetitSuisse Posted February 27, 2015 Share Posted February 27, 2015 I have the same book, and after spending ages trying to see if I made a type, google brought me here and ended my frustrations. The amazon reviews for this book are awful, but we're using it in class, and although there are many errors the structure of the book is pretty good, and just like you, I ended up learning something because of the errors. My solution ended up working like this: <?php if ( ($_POST['val1']=="") || ($_POST['val2']=="") || ($_POST['calc']=="") ) { header("Location:calcform.php"); exit; } 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']; } ?> <!DOCTYPE html> <head> <title>Calculation Result</title> </head> <body> <p>The result of the calculation is <?php echo "$result"; ?>!</p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/141124-help-with-this-simple-calculator/#findComment-1506994 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.