Jump to content

Problem with php code simple calculater


UaE

Recommended Posts

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: 3

the output should be :

12 * 3 = 36

and it should appear in the same page (calculator page)

here is a picture>

0GraB.jpg

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>

Link to comment
https://forums.phpfreaks.com/topic/280119-problem-with-php-code-simple-calculater/
Share on other sites

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:

 

xipED.jpg

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>

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 !

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.