ahsn Posted May 26, 2013 Share Posted May 26, 2013 I am a newbie in php and learning it eagerly. Anyway, I would like to make a program which will take two numbers from users and then show the result at the same page. Could anyone please show me the coding to do that? Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/ Share on other sites More sharing options...
davidannis Posted May 26, 2013 Share Posted May 26, 2013 My son wrote this (with a little help from his dad): <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Calculator</title> </head> <body> <p> <form method="POST" action="Calculator.php"> <input type="text" name="x"><br /> <select name="operation"> <option value="+">+</option> <option value="-">minus</option> <option value="*">*</option> <option value="/">/</option> <option value="sqrt">square root</option> </select><br /> <input type="text" name="z"> <input type="submit"> </form> </p> <?php switch ($_POST["operation"]){ case '+': $answer=$_POST['x']+$_POST['z']; break ; case '-': $answer=$_POST['x']-$_POST['z']; break ; case '*': $answer=$_POST['x']*$_POST['z']; break ; case '/': $answer=$_POST['x']/$_POST['z']; break ; case 'sqrt': $answer=sqrt($_POST['x']); break ; } echo $answer; ?> </body> </html> any errors are mine. Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1432415 Share on other sites More sharing options...
ignace Posted May 27, 2013 Share Posted May 27, 2013 (edited) @davidannis, who's your dad? Edited May 27, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1432553 Share on other sites More sharing options...
Jessica Posted May 27, 2013 Share Posted May 27, 2013 Nitpick: The OP didn't specify that he wanted a calculator. He said he wanted to take two numbers and show the result. The result of WHAT is yet to be defined. Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1432557 Share on other sites More sharing options...
davidannis Posted May 28, 2013 Share Posted May 28, 2013 @davidannis, who's your dad? Mr. Annis, of course. Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1432639 Share on other sites More sharing options...
davidannis Posted May 28, 2013 Share Posted May 28, 2013 Nitpick: The OP didn't specify that he wanted a calculator. He said he wanted to take two numbers and show the result. The result of WHAT is yet to be defined. Hopefully, if it's not one of the operations my 7 year old coded the OP can add another case to the switch statement. If not, perhaps my son can do it as a freelance job. Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1432642 Share on other sites More sharing options...
ahsn Posted May 28, 2013 Author Share Posted May 28, 2013 @davidannis, thank you very much for the codes. Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1432772 Share on other sites More sharing options...
khyzer Posted June 18, 2013 Share Posted June 18, 2013 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Calculator</title> </head> <body> <p> <form method="POST" action=""> First Number : <input type="text" name="x"><br /> Oprator :<select name="operation"> <option value="+">+</option> <option value="-">minus</option> <option value="*">*</option> <option value="/">/</option> <option value="sqrt">square root</option> </select><br /> Second Number :<input type="text" name="z"> <input type="submit" name="result" value="result"> </form> </p> <?php if(isset($_POST['result'])) { switch ($_POST["operation"]){ case '+': $answer="Sum is : " . ($_POST['x']+$_POST['z']); break ; case '-': $answer="Subtraction is : " .($_POST['x']-$_POST['z']); break ; case '*': $answer="Multiplication is : " .($_POST['x']*$_POST['z']); break ; case '/': $answer="Division is : " .($_POST['x']/$_POST['z']); break ; case 'sqrt': $answer= "Sqrt of first input is : " .(sqrt($_POST['x'])); break ; } echo $answer; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/278410-simple-summation-program/#findComment-1436550 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.