Jump to content

Simple Calculator Not Outputting Result

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Maxxamo

Maxxamo

    Newbie

  • Members
  • PipPip
  • 14 posts
So I thought I'd start my PHP development career on my own (with no vid tutorials or anything) by creating a very simple calculator.

However, everything seems to be working EXCEPT the result. So, just the most important part of the app. I've attached my input.php and output.php, as well as my functions.php.

If someone could tell me why its not giving me a value I'll personally send you a beer! :-P

Input.php:

<html>


<head>


<title>Input your Calculator Values</title>

<b>Input your Calculator Values</b>

</head>


<body>

<br>

<br>

<form name="calculate" action="output.php" method="post">

First Number:

<input type="text" name="var1" value="0" /><br>

Second Number:

<input type="text" name="var2" value="0" /><br>

<input type="submit" name="calculate" value="Add" />

<input type="submit" name="calculate" value="Subtract" />

<input type="submit" name="calculate" value="Multiply" />

<input type="submit" name="calculate" value="Divide" />

</form> 


</body>




<?php 

		include ("navigation.php");

?>

</body>

</html>

Output.php:

<?php

	require_once("functions.php");

?>

<html>


<head>

<title>Results of your Calculation</title>


<b>Results of your Calculation</b><br>



</head>


<body>

<?php

	

	$var1 = $_POST[var1];

	$var2 = $_POST[var2];

	

	if ($_POST[calculate] == "Add")	{

		add($var1, $var2);

		echo $value;

		

}	elseif ($_POST[calculate] == "Subtract") {

		subtract($var1, $var2);

		echo $value;

		

}	elseif ($_POST[calculate] == "Multiply") {

		multiply($var1, $var2);

		echo $value;

		

}	elseif ($_POST[calculate] == "Divide") {

		divide($var1, $var2);

		echo $value;

		

}	else {

		echo "Something went terribly terribly wrong.";

}

	echo "Your result: " . $value . "<br>";

	

?>



<?php 

		include ("navigation.php");

?>

</body>


</html>

Functions.php:

<?php


	function add($var1, $var2)	{

		$value = $var1 + $var2;

		return $value;

		}


	function subtract($var1, $var2) {

		$value = $var1 - $var2;

		return $value;

		}

		

	function divide($var1, $var2) {

		$value = $var1 / $var2;

		return $value;

		}

	

	function multiply($var1, $var2) {

		$value = $var1 * $var2;

		return $value;

		}

		

?>

$value doesn't seem to be setting, and I'm not entirely sure why not.

Thanks again for your help. I feel like I'm the only one using this board.. so I apologize if you all get to know me pretty fast.. LOL!

#2
Maxxamo

Maxxamo

    Newbie

  • Members
  • PipPip
  • 14 posts
To see this live:

Input your Calculator Values

#3
Maxxamo

Maxxamo

    Newbie

  • Members
  • PipPip
  • 14 posts
I've since made the code functional, and it works. However, I've kind of oddly done the functions to echo within the function; therefore its rather messy on the output.php page. I'll keep the example code I pasted in the original post in order to solicit suggestions on ways to make it cleaner for future use.

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Be sure to use single quotes, var1, var2, ect... are not constants.

$_POST['var1']


#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
also, your $value won't get a value, as it's within a function it is sat.

if ($_POST[calculate] == "Add")    { 
        add($var1, $var2); 
        echo $value;
should be:
if ($_POST['calculate'] == "Add")    { 
        $value = add($var1, $var2);  

then, your functions could be:

function add($var1, $var2)    { 
        return $var1 + $var2;  
}
as the middle storing isn't needed. it's ok to do it, but $value can't be used outside the function, unless global vars is turned on, which it isn't in php now.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
An interesting approach to building a calculator is to use callbacks. Here is your entire calculator simplified:

<html> 
<head> 
<title>Input your Calculator Values</title> 
</head> 

<body> 
<?php  

if(isset($_POST['var1'], $_POST['var2'], $_POST['calculate'])) {
    echo "Result: " . call_user_func($_POST['calculate'], $_POST['var1'], $_POST['var2']);
}

function add($var1, $var2) { 
    return $var1 + $var2; 
} 

function subtract($var1, $var2) { 
    return $var1 - $var2; 
} 

function multiply($var1, $var2) { 
     return $var1 * $var2;
}

function divide($var1, $var2) { 
    return $var1 / $var2;  
} 
     
?> 
<br />
<strong>Input your Calculator Values</strong>  
<form name="calculate" action="index.php" method="post"> 
First Number: 
<input type="text" name="var1" value="0" /><br> 
Second Number: 
<input type="text" name="var2" value="0" /><br> 
<input type="submit" name="calculate" value="add" /> 
<input type="submit" name="calculate" value="subtract" /> 
<input type="submit" name="calculate" value="multiply" /> 
<input type="submit" name="calculate" value="divide" /> 
</form>  

</body> 
 
</body> 
</html>

even more interesting
<html> 
<head> 
<title>Input your Calculator Values</title> 
</head> 

<body> 
<?php  

if(isset($_POST['var1'], $_POST['var2'], $_POST['calculate'])) {
	eval("echo ".$_POST['var1'].$_POST['calculate'].$_POST['var2'].";");
}
     
?> 
<br />

<strong>Input your Calculator Values</strong>  
<form name="calculate" action="index.php" method="post"> 
First Number: 
<input type="text" name="var1" value="0" /><br> 
Second Number: 
<input type="text" name="var2" value="0" /><br> 
<input type="submit" name="calculate" value="+" /> 
<input type="submit" name="calculate" value="-" /> 
<input type="submit" name="calculate" value="*" /> 
<input type="submit" name="calculate" value="/" /> 
</form>  

</body> 
 
</body> 
</html>

However, both approaches (especially the latter) open up big security vulnerabilities.