View Full Version : Form button not passing its value

11-04-2009, 02:43 PM

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:


// grab the form values from $_HTTP_POST_VARS hash

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; }

{ $x=0;
$prod=0; }

return $prod;


Calculator Form

<title>PHP Calculator Example</title>


<h3>PHP Calculator</h3>
<p>Enter two numbers, select the required opperation and click calculate to retrieve the answer.</p>
<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"/>

<hr width="75%" color="red">

<!-- print the result -->
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";}

$c = new Calculator;
{$prod = $c->calculate($x,$opp,$y);
print "<p>$x $opp $y = $prod</p>";}

} ?>

11-04-2009, 02:54 PM
Welcome to the forum. Please edit your first post to enclose your code in
tags. It makes things much easier on the eyes. :D

Now, as for the buttons, as far as I am aware forms should be using <input type="submit" ......./> elements for sending form data. This is what I routinely use and I never have issues with it. The submit button really only submits the name of the button and the value on the button. So, for example, this button:
<input type="submit" name="this_submit_button" value="Go!" />

would pass this variable data:

That comes in handy if you have more than one type of submit button (such as "save and add another" vs. "save and go to next step" buttons) and need to pay attention to the user's intended submit type.

Now, if you simply want to pass other ancillary variables you should either be keeping/setting session variables or else using hidden input elements to pass the data like so:

<input type="hidden" name="this_variable" value="the_data" />

and on a successful submission this would pass this_variable=the_data to the server.

11-04-2009, 03:57 PM
I understand what you are saying about using <input type="submit" name="opp" value="+"/> however any input type="submit" will run the form method and action once clicked.

Obviously when using a calculator the user will enter an x value then click one of the operation buttons (+, -, x, / etc) then they will enter their y value and click '=' (Currently my '=' button is input type="submit") if I make any other button submit it will send the data to early if you follow me?

I cannot use hidden input fields as the operation must be selected by the user.

I will upload the working version (which uses a drop down menu to select the operation) to the following link - Working calculator (http://www.jh-webdesign.com/uni/calc1.php)

Here is the version which isnt working with the operation selected by buttons - Button calculator (http://www.jh-webdesign.com/uni/calcbuttons.php)

Thanks in advance.

11-04-2009, 04:50 PM
I would be using javascript to set input values dynamically, myself. Is there any reason that this is impossible for your application?

Besides which, I don't think that the regular type="button" input has any direct effect in HTML forms - it was pretty much just develped as a javascript vehicle. I might be dead wrong about that, but this is what I have heard.

11-04-2009, 05:58 PM
There is no particular reason why I couldn't use JavaScript but I am very novice at that too. It just seems like a error in logic to me that php can pull values from the radio button, drop down list, text or check box input methods but not a simple button?

Im trying to teach myself PHP so if at all possible I wanted to overcome any problem I came across as opposed to by-passing it by using JavaScript or similar.

Thank you for trying tho :thumbsup:

11-04-2009, 09:27 PM
Hey there.
I'm no Pro, but here's some basic fodder the input vs button debate.
Based on w3schools, it kinda sounds like input (http://www.w3schools.com/tags/tag_input.asp) may be preferred over button (http://www.w3schools.com/tags/tag_button.asp).

Now as for buttons (whether via <input> or <button>), aren't they and actionable element?
If my presumption is correct, then the buttons are working exactly how they are supposed to. They're doing nothing :)
What about getting rid of the "=" button, and make each of the operator buttons into submit buttons.
Rowsdower kinda touched on this with:

...That comes in handy if you have more than one type of submit button ...
In essence, you would just change the work flow

1) X value
2) operator
3) Y value
4) "="

1) X value
2) Y value
3) operator


~ Mo

11-04-2009, 11:21 PM
Thanks Mo,

I had thought about that but its a last ditch method for me, basically because I want to make it as close to a real life physical calculator as possible....but it would seem PHP doesn't really make this easy...or perhaps even possible....

Perhaps I should go and learn some C lol

11-04-2009, 11:26 PM
On further reflection, it seems to me as though you really could do this with all submit buttons and a bunch of conditional tests. It would load the page with every button press and it would be very slow and annoying, but it could technically be done the way you are asking about.

Really, what you're limited by here is the HTML form, not the PHP. But really, HTML was not meant to be dynamic on its own anyway. Your problem lies on the client-side and no amount of server-side-only solution can fix the problem entirely.