Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Form button not passing its value

    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:

    Code:
    <?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

    Code:
    <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>
    Last edited by HARVS1789UK; 11-04-2009 at 02:42 PM.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Welcome to the forum. Please edit your first post to enclose your code in [CODE][/CODE] tags. It makes things much easier on the eyes.

    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:
    Code:
    <input type="submit" name="this_submit_button" value="Go!" />
    would pass this variable data:
    this_submit_button=Go!

    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:
    Code:
    <input type="hidden" name="this_variable" value="the_data" />
    and on a successful submission this would pass this_variable=the_data to the server.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

    Here is the version which isnt working with the operation selected by buttons - Button calculator

    Thanks in advance.

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I would be using javascript to set input values dynamically, myself. Is there any reason that this is impossible for your application?

    Edit: 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.
    Last edited by Rowsdower!; 11-04-2009 at 04:20 PM. Reason: Afterthoughts
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    HARVS1789UK (11-04-2009)

  • #5
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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 may be preferred over button.

    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:
    Quote Originally Posted by Rowsdower! View Post
    ...That comes in handy if you have more than one type of submit button ...
    In essence, you would just change the work flow
    from:
    1) X value
    2) operator
    3) Y value
    4) "="
    to:
    1) X value
    2) Y value
    3) operator

    HTH.

    ~ Mo

  • #7
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    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.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •