HARVS1789UK
11-04-2009, 01:43 PM
Hi,
Im new to PHP and am currently making a basic calculator, it is working perfectly when I use a drop down menu to select the operation (addition, subtraction etc) however I now want to change it so that you select the operation by clicking the appropriate button. (like a real calculator) The actual calculation is done in the class - calculator and I am using extract ($_GET) to pull in all the values from the form.
I have used <button.....> </button> as I have read it is more functional then <input type="button" etc...> but I have tried using both methods yet the PHP is unable to receive any value from any of the buttons.
If I change the operation selection back to a drop down or even radio button it works perfectly, surly a form button should also be able to pass PHP a value? I would rather not use javascript etc as I want to keep strictly to PHP.
Code is below, any help would be brilliant. Thank You.
Calculator Class:
<?php
// grab the form values from $_HTTP_POST_VARS hash
extract($_GET);
class calculator
{
public function calculate ($x, $opp, $y) {
// first compute the output, but only if data has been input
if ($opp=="+")
{ $prod = $x + $y; }
elseif ($opp=="-")
{ $prod = $x - $y; }
elseif ($opp=="X")
{ $prod = $x * $y; }
elseif ($opp=="/")
{ $prod = $x / $y; }
elseif ($opp=="^")
{ $prod = pow($x,$y); }
elseif ($opp=="hyp")
{
$x = pow($x,2);
$y = pow($y,2);
$z = $x + $y;
$prod = sqrt($z); }
elseif ($opp=="%")
{ $prod = $x%$y; }
else
{ $x=0;
$y=0;
$prod=0; }
return $prod;
}
}
?>
Calculator Form
<html>
<head>
<title>PHP Calculator Example</title>
</head>
<body>
<h3>PHP Calculator</h3>
<p>Enter two numbers, select the required opperation and click calculate to retrieve the answer.</p>
<center>
<form method="get" action="<?php print $_SERVER[PHP_SELF]; ?>">
x = <input type="text" name="x" size="5" value="<?php print $x; ?>"/>
<input type="button" name="opp" value="+"/>
<input type="button" name="opp" value="-"/>
<input type="button" name="opp" value="X"/>
<input type="button" name="opp" value="/"/>
<input type="button" name="opp" value="^"/>
<input type="button" name="opp" value="hyp"/>
<input type="button" name="opp" value="%"/>
y = <input type="text" name="y" size="5" value="<?php print $y; ?>"/>
<br /><br />
<input type="submit" name="calc" value="="/>
<input type="reset" name="clear" value="C"/>
</form>
<hr width="75%" color="red">
<!-- print the result -->
<?php
include ('calculator.class.php');
if(isset($calc)) {
if (!is_numeric ($x))
{echo "X needs to be a integer";}
elseif (!is_numeric ($y))
{echo "Y needs to be a integer";}
else
$c = new Calculator;
{$prod = $c->calculate($x,$opp,$y);
print "<p>$x $opp $y = $prod</p>";}
} ?>
</center>
</body>
</html>
Im new to PHP and am currently making a basic calculator, it is working perfectly when I use a drop down menu to select the operation (addition, subtraction etc) however I now want to change it so that you select the operation by clicking the appropriate button. (like a real calculator) The actual calculation is done in the class - calculator and I am using extract ($_GET) to pull in all the values from the form.
I have used <button.....> </button> as I have read it is more functional then <input type="button" etc...> but I have tried using both methods yet the PHP is unable to receive any value from any of the buttons.
If I change the operation selection back to a drop down or even radio button it works perfectly, surly a form button should also be able to pass PHP a value? I would rather not use javascript etc as I want to keep strictly to PHP.
Code is below, any help would be brilliant. Thank You.
Calculator Class:
<?php
// grab the form values from $_HTTP_POST_VARS hash
extract($_GET);
class calculator
{
public function calculate ($x, $opp, $y) {
// first compute the output, but only if data has been input
if ($opp=="+")
{ $prod = $x + $y; }
elseif ($opp=="-")
{ $prod = $x - $y; }
elseif ($opp=="X")
{ $prod = $x * $y; }
elseif ($opp=="/")
{ $prod = $x / $y; }
elseif ($opp=="^")
{ $prod = pow($x,$y); }
elseif ($opp=="hyp")
{
$x = pow($x,2);
$y = pow($y,2);
$z = $x + $y;
$prod = sqrt($z); }
elseif ($opp=="%")
{ $prod = $x%$y; }
else
{ $x=0;
$y=0;
$prod=0; }
return $prod;
}
}
?>
Calculator Form
<html>
<head>
<title>PHP Calculator Example</title>
</head>
<body>
<h3>PHP Calculator</h3>
<p>Enter two numbers, select the required opperation and click calculate to retrieve the answer.</p>
<center>
<form method="get" action="<?php print $_SERVER[PHP_SELF]; ?>">
x = <input type="text" name="x" size="5" value="<?php print $x; ?>"/>
<input type="button" name="opp" value="+"/>
<input type="button" name="opp" value="-"/>
<input type="button" name="opp" value="X"/>
<input type="button" name="opp" value="/"/>
<input type="button" name="opp" value="^"/>
<input type="button" name="opp" value="hyp"/>
<input type="button" name="opp" value="%"/>
y = <input type="text" name="y" size="5" value="<?php print $y; ?>"/>
<br /><br />
<input type="submit" name="calc" value="="/>
<input type="reset" name="clear" value="C"/>
</form>
<hr width="75%" color="red">
<!-- print the result -->
<?php
include ('calculator.class.php');
if(isset($calc)) {
if (!is_numeric ($x))
{echo "X needs to be a integer";}
elseif (!is_numeric ($y))
{echo "Y needs to be a integer";}
else
$c = new Calculator;
{$prod = $c->calculate($x,$opp,$y);
print "<p>$x $opp $y = $prod</p>";}
} ?>
</center>
</body>
</html>