Jump to content

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>

Edited by UaE

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 by UaE

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>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.