happy02134 Posted February 27, 2013 Share Posted February 27, 2013 I coded this form calculator using php..while there are no syntax errors, I have messed something up in the coding and I cannot figure out what. Everything works however each button does not do it's job. i.e. when i press * for multiply it adds and same for all the other buttons. It only adds: below is my code : <?php if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] =="")) if ($_POST["calc"] = "+") { $result = $_POST[num1] + $_POST[num2]; } else if ($_POST[calc] == "-") { $result = $_POST[num1] - $_POST[num2]; } else if ($_POST[calc] == "*") { $result = $_POST[num1] * $_POST[num2]; } elseif ($_POST[calc] == "/") { $result = $_POST[num1] / $_POST[num2]; } ?> <!doctype html> <html lang="eng"> <head> <meta charset="utf-8"> <title>Multiply</title> </head> <body style="background-color:lightgrey;"> <div id="header"> <h1>Multiply</h1> <hr /> </div> <div id="content"> <form name="calc" action="multiply.php" method="post"> <p>Number 1: <input type="text" name="num1"></p> <p>Number 2: <input type="text" name="num2"></p> <input type='submit' name='calc' value='+'> <input type='submit' name='calc' value='-'> <input type='submit' name='calc' value='*'> <input type='submit' name='calc' value='/'> <?php echo "<p>The result of the calculation is: $result</p>"; ?> </form> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
requinix Posted February 27, 2013 Share Posted February 27, 2013 (edited) Please use [php] or [code] tags for your PHP code. Striving for no syntax errors is really great, but you should avoid warnings and notices too. Your script has a few of them. help with $-Post function$_POST is a variable, not a function. if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] =="")) - If I haven't submitted the form yet then $_POST will not contain anything. You should check that these things actually exist before trying to use them. - For anything other than a trivial if block you should use {}s. if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] =="")) // and almost everywhere else you use $_POST val1, val2, and calc are not constants. If you want strings then you need to use quotes. if ($_POST["calc"] = "+") { Count your equals signs. Edited February 27, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
teynon Posted February 28, 2013 Share Posted February 28, 2013 Among all the corrections requinix has made, the pointing out of if ($_POST["calc"] = "+") { is to say that you are using one = sign which is assignment. Change it to if ($_POST["calc"] == "+") { Quote Link to comment Share on other sites More sharing options...
happy02134 Posted February 28, 2013 Author Share Posted February 28, 2013 (edited) Thanks for the help. I understand the $_Post variable now and fixed my issues. I am just a little confused still about the output. For example, I am currently adding the is_numeric () function. I coded it like this: <?php if (is_numeric ("num1 . num2")) { echo ""; } else { echo "Numbers only please"; } ?> However, "numbers only please" appears on the page before any values are even entered. I noticed it has done that before. I added this: echo "<p>The result of the calculation is: $result</p>"; under my form so the answer would appear there when clicked but it always appears there even before any buttons are clicked. Shouldn't it appear only when I click the button? I am new to PHP, and everything I am reading tells me to uses those variables/functions. Should I be using a different approach to achieve that? Edited February 28, 2013 by happy02134 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 28, 2013 Share Posted February 28, 2013 "num1 . num2" will always be a string, you just wrote a string. That's like asking if "bob . john" is numeric. Never. Quote Link to comment Share on other sites More sharing options...
happy02134 Posted February 28, 2013 Author Share Posted February 28, 2013 "num1 . num2" will always be a string, you just wrote a string. That's like asking if "bob . john" is numeric. Never. Yeah I had realized that after and fixed it. Still wont work though Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 28, 2013 Share Posted February 28, 2013 You fixed it *how*? Quote Link to comment Share on other sites More sharing options...
happy02134 Posted February 28, 2013 Author Share Posted February 28, 2013 (edited) You fixed it *how*? <?php /* Name: Brittany Hafer Date: Feb. 28th 2013 Purpose: Homework 2 */ $num1 = $_POST['num1']; $num2 = $_POST['num2']; $result = $num1 + $num2; ?> <!doctype html> <html lang="eng"> <head> <meta charset="utf-8"> <title>Add</title> </head> <body style="background-color:lightgrey;"> <div id="header"> <h1>Add</h1> <hr /> </div> <div id="content"> <form name="frmInput" action="add.php" method="post"> <p>Number 1: <input type="text" name="num1"></p> <p>Number 2: <input type="text" name="num2"></p> <p><input type='submit' name='btnSubmit' value='Add'></p> <?php echo "$num1 plus $num2 is " . number_format("$result",2); if (is_numeric ("text")) { echo ""; } else { echo "Numbers only please"; } ?> </form> </div> <div id="footer"> <hr /> <cite>Created for MIS3501</cite> </div> </body> </html> I didn't fix my problems I am having but I changed the string. I just don't fully understand how to use these functions and variables Edited February 28, 2013 by happy02134 Quote Link to comment Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 I imagine it would have fixed most, if not all, of your problems since you're using completely different code. is_numeric() wants something to check. If you tell it to look at the string "text" then it's going to respond with "no, that is not numeric". Quote Link to comment Share on other sites More sharing options...
happy02134 Posted February 28, 2013 Author Share Posted February 28, 2013 I imagine it would have fixed most, if not all, of your problems since you're using completely different code. is_numeric() wants something to check. If you tell it to look at the string "text" then it's going to respond with "no, that is not numeric". it doesn't respond. "no, that is not numeric" just constantly appears on the page Quote Link to comment Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 The homework is due today. Between here and Dev Shed it seems you need more personalized attention than we can give over the Internet. I suggest you set up an appointment with Mr. Shafer (apparently he's appointment only 12-1pm) and turn in the assignment a day or two late. But be sure to turn it in within a week: you'll lose many more points if you don't submit it at all. Quote Link to comment 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.