UaE Posted July 13, 2013 Share Posted July 13, 2013 Hello,I have a problem writing a code in one file. It's a drop list having a different operation ( - + / * ) I have to select one of these and before that there are two text field to write two number. assuming I choosed ( * ) and write in number1: 12 and number2: 3the output should be :12 * 3 = 36and it should appear in the same page (calculator page)here is a picture>this is my code and help me to improve it! <html> <head><title> test </title> </head> <body> <h1> Enter Comment </h1> <form action="new.php" method="get"> Number1: <input type="text" name="num1"> Number2: <input type="text" name="num2"> <select name="cal"> <option> + </option> <option> - </option> <option> / </option> <option> * </option> </select> <input type="submit" value="Do Math"> <?php $cal = ""; if ( $_GET["cal"] == + ) echo $_GET["num1"] ."+". $_GET["num2"] ."=". $_GET["num1"] + $_GET["num2"]; if ( $_GET["cal"] == - ) echo $_GET["num1"] - $_GET["num2"]; if ( $_GET["cal"] == / ) echo $_GET["num1"] / $_GET["num2"]; if ( $_GET["cal"] == * ) echo $_GET["num1"] * $_GET["num2"]; setcookie ("cal" , "" , time()+3600); ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/ Share on other sites More sharing options...
trq Posted July 13, 2013 Share Posted July 13, 2013 The values stored within the $_GET array will always be strings. So, you need to change: if ( $_GET["cal"] == + ) to if ($_GET["cal"] == '+') Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440582 Share on other sites More sharing options...
UaE Posted July 13, 2013 Author Share Posted July 13, 2013 (edited) I changed it. but my problem is that I want Each time user press Do Math, it will be added to the screen like this php code: whenever you write the comment , it comes down when you press Add comment. This is my idea. this is my last code: <html> <head><title> test </title> </head> <body> <h1> Enter Comment </h1> <form action="a.php" method="get"> Number1: <input type="text" name="num1"> Number2: <input type="text" name="num2"> <select name="cal"> <option> + </option> <option> - </option> <option> / </option> <option> * </option> </select> <input type="submit" value="Do Math"> <?php $result= ""; if ( $_GET["cal"] == '+' ) $result = $_GET["num1"] ."+". $_GET["num2"] ."=". $_GET["num1"] + $_GET["num2"]; if ( $_GET["cal"] == '-' ) $reslut = $_GET["num1"] - $_GET["num2"] ."=". $_GET["num1"] - $_GET["num2"]; if ( $_GET["cal"] == '/' ) $result = $_GET["num1"] / $_GET["num2"] ."=". $_GET["num1"] / $_GET["num2"]; if ( $_GET["cal"] == '*' ) $result = $_GET["num1"] * $_GET["num2"] ."=". $_GET["num1"] * $_GET["num2"]; setcookie ("result" , "$result" , time()+3600); ?> </form> </body> </html> Edited July 13, 2013 by UaE Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440587 Share on other sites More sharing options...
web_craftsman Posted July 14, 2013 Share Posted July 14, 2013 $result = $_GET["num1"] . $_GET["cal"] . $_GET["num2"]; if ( $_GET["cal"] == '+' ) $result .= ($_GET["num1"] + $_GET["num2"]); // and so on If you want to show previous math calculations you need to save them into session Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440659 Share on other sites More sharing options...
UaE Posted July 14, 2013 Author Share Posted July 14, 2013 (edited) I almost solve it but have some wrongs. <!DOCTYPE html> <html> <head><title> test </title> </head> <body> <h1> Enter Comment </h1> <form action="" method="get"> Number1: <input type="text" name="num1"> Number2: <input type="text" name="num2"> <select name="cal"> <option value="+"> + </option> <option value="-"> - </option> <option value="/"> / </option> <option value="*"> * </option> </select> <input type="submit" value="Do Math"> </form> <?php $cal = ""; if(isset($_GET["cal"]) and isset($_GET["num1"]) and isset($_GET["num2"])) { if ( $_GET["cal"] == "+" ) echo (string)$_GET["num1"] ."+". (string)$_GET["num2"] ."=".((string)($_GET["num1"] + $_GET["num2"])); if ( $_GET["cal"] == "-" ) echo (string)$_GET["num1"] ."-". (string)$_GET["num2"] ."=".((string)($_GET["num1"] - $_GET["num2"])); if ( $_GET["cal"] == "/" ) echo (string)$_GET["num1"] ."/". (string)$_GET["num2"] ."=".((string)($_GET["num1"] / $_GET["num2"])); if ( $_GET["cal"] == "*" ) echo (string)$_GET["num1"] ."*". (string)$_GET["num2"] ."=".((string)($_GET["num1"] * $_GET["num2"])); } setcookie ("cal" , "" , time()+3600); echo $cal; ?> </form> </body> </html> If I entered 5 and 6 and press Do Math it will output 5+6=11 but If I do the process again the first output will disappear which is wrong ! Edited July 14, 2013 by UaE Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440676 Share on other sites More sharing options...
trq Posted July 14, 2013 Share Posted July 14, 2013 first problem, in the output, assuming I entered 1 and 5 with plus sign the output will look like this > 1 + 56 there is no space and equal sign '=' You haven't told the program to do that. second problem, If I entered other numbers and do the process again, the first output will disappear. which is wrong This has already been answered. Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440678 Share on other sites More sharing options...
UaE Posted July 14, 2013 Author Share Posted July 14, 2013 how to make a sessions to see all process that I have done in the same page !? Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440717 Share on other sites More sharing options...
Barand Posted July 14, 2013 Share Posted July 14, 2013 try <?php session_start(); $cal = ""; if(isset($_GET["cal"]) and isset($_GET["num1"]) and isset($_GET["num2"])) { if ( $_GET["cal"] == "+" ) $cal = $_GET["num1"] ."+". $_GET["num2"] ."=".(($_GET["num1"] + $_GET["num2"])); elseif ( $_GET["cal"] == "-" ) $cal = $_GET["num1"] ."-". $_GET["num2"] ."=".(($_GET["num1"] - $_GET["num2"])); elseif ( $_GET["cal"] == "/" ) $cal = $_GET["num1"] ."/". $_GET["num2"] ."=".(($_GET["num1"] / $_GET["num2"])); elseif ( $_GET["cal"] == "*" ) $cal = $_GET["num1"] ."*". $_GET["num2"] ."=".(($_GET["num1"] * $_GET["num2"])); $_SESSION['results'][] = $cal; // store in session array } ?> <!DOCTYPE html> <html> <head><title> test </title> </head> <body> <h1> Enter Comment </h1> <form action="" method="get"> Number1: <input type="text" name="num1"> Number2: <input type="text" name="num2"> <select name="cal"> <option value="+"> + </option> <option value="-"> - </option> <option value="/"> / </option> <option value="*"> * </option> </select> <input type="submit" value="Do Math"> </form> <?php // print stored session array if (isset($_SESSION['results'])) { foreach ($_SESSION['results'] as $res) { echo $res . '<br>'; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/#findComment-1440724 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.