octacon Posted February 9, 2008 Share Posted February 9, 2008 hey i'm completely new and would like some expert advice. though my problem is probably relatively a simple one I can't seem to get this calculator working. I was hoping to get some help or at least advice on what i am doing wrong here. I've pasted the code I have below. I have been trying to get it work the past couple hours...sigh...i hope someone here can at least point me in the right direction. Thanks. <html> <head> <meta http-equiv="Content-Language" content="en-ca"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Simple Calculator</title> </head> <body> <form method="POST" action="LabO2.php"> <p align="center"><b>Number 1: </b><input type="text" name="number1" size="10"></p> <p align="center"><b>Number 2: </b> <input type="text" name="number2" size="10"></p> <p align="center"><b>Result: </b> <?php echo ($result); ?> <p align="center"> <input type="submit" value="+" name="func"> <input type="submit" value="-" name="func"> <input type="submit" value="*" name="func"> <input type="submit" value="/" name="func"> <?php if($do == "+"){ $result = $POST['number1' + $POST['number2']; } if($do == "-"){ $result = $POST['number1' - $POST['number2']; } if($do == "*"){ $result = $POST['number1' * $POST['number2']; } if($do == "/"){ $result = $POST['number1' / $POST['number2']; } ?> </p> <p align="center"><input type="reset" value="Clear"></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/ Share on other sites More sharing options...
kratsg Posted February 9, 2008 Share Posted February 9, 2008 Gotta add some error handlers: <?php $_POST['number1'] = $_POST['number1']*1; $_POST['number2'] = $_POST['number2']*1; if(is_numeric($_POST['number1']) && is_numeric($_POST['number2']){//if both are numeric if($do == "+"){ $result = $POST['number1'] + $POST['number2']; } if($do == "-"){ $result = $POST['number1'] - $POST['number2']; } if($do == "*"){ $result = $POST['number1'] * $POST['number2']; } if($do == "/"){ $result = $POST['number1'] / $POST['number2']; } } else {//at least one is not numeric die("One of your entries is not a valid number! Please try again."); } This will multiply the user submissions by 1 (automatically converting from string to number if plausible). The last thing I added was brackets around $_POST['number1'] in the $result areas, as you forgot that second bracket. Quote Link to comment https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/#findComment-462511 Share on other sites More sharing options...
octacon Posted February 9, 2008 Author Share Posted February 9, 2008 I actually changed the $do to $func on there so I know they are mismatched. I pasted a copy of the code while I was editiing it by mistake...the problem remains the same I can't print the results. Ideally I'd want the printed on the same form in a textbox beside Result: but right now I would just settle for it to show up at all. Quote Link to comment https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/#findComment-462512 Share on other sites More sharing options...
kratsg Posted February 9, 2008 Share Posted February 9, 2008 It may be the multiple submits, try using a dropdown instead: <select name="do"> <option value="+">Add</option> <option value="-">Subtract</option> <option value="*">Multiply</option> <option value="/">Divide</option> </select> <input type="submit"> Quote Link to comment https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/#findComment-462515 Share on other sites More sharing options...
octacon Posted February 9, 2008 Author Share Posted February 9, 2008 Thanks for your advice didn't notice the missing brackets and I tried the drop down box and I still can't get the results to shop up. Quote Link to comment https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/#findComment-462517 Share on other sites More sharing options...
kratsg Posted February 9, 2008 Share Posted February 9, 2008 Now that we've fixed your coding flaws, let's fix the logical flaw. We're going to check to see if it was posted, if it was, handle everything, THEN echo (don't just echo then handle everything..) <?php if($_POST && !empty($_POST)){ //insert the do/func functions for adding multiplying and subtracting } else {$result = null;}//nullify if they didn't post ?> //HTML IN HERE Quote Link to comment https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/#findComment-462892 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.