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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2013
    Location
    South Africa
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy php My result not displaying after form is submitted

    Hi, I have created a form in OOP class Calculator, but when I enter the two numbers and submit the form it does not display the result.
    I cannot figure where my problem is. Either with the php code or the html code. Any help will do.

    PHP Code:
    <?php

    class Calculator {
    private 
    $_val1$_val2;
    public 
    $result="";

    public function 
    add() {
    return 
    $this->_val1 $this->_val2;
    }

    public function 
    subtract() {
    return 
    $this->_val1 $this->_val2;
    }

    public function 
    multiply() {
    return 
    $this->_val1 $this->_val2;
    }

    public function 
    divide() {
    return 
    $this->_val1 $this->_val2;

    function 
    getResult() {
    echo 
    $this->return;
    }
    }

    }
    ?>
    <?php
     error_reporting
    (0);
      if (
    $_REQUEST['clear']) {
           
    $_val1 ="";
        
    $_val2 ="";
        
    $result ="";
        }
     if (
    $_REQUEST['submit']) 
         {
            
    $radios $_REQUEST['radios'];          
        
        if (
    $radios == "add") {
             
    $result = new Calculator();
             
    $result->add();
            }
        elseif (
    $radios == "subtract") {
            
    $result = new Calculator();
            echo 
    $result->subtract();
            }
        elseif (
    $radios == "multiply") {
            
    $result = new Calculator();
            echo 
    $result->multiply();
            }
        elseif (
    $radios == "divide") {
            
    $result = new Calculator();
            echo 
    $result->divide();
            } 
    }

    ?>
    ,
    PHP Code:
    <!-- create the form -->
    <form name="Form1" method="POST" action="">

    <p align="left"><b>Enter the first number :</b><br>
    <input type="text" name="_val1" size="20" value="<?php $_val1?>"><br>
     
    </p>
    <p align="left"><b>Enter the second number : </b><br>
    <input type="text" name="_val2" size="20" value="<?php $_val2?>"><br></p>

    <p align="left">
        <input type="radio" name="radios" value="add">Addition <br>
        <input type="radio" name="radios" value="subtract">Subtraction <br>
        <input type="radio" name="radios" value="multiply">Multiplication <br>
        <input type="radio" name="radios" value="divide">Division <br><br>
     </p>
       <p align="left"><strong>Result</strong>
        <input type="text" name="result" value="<?php $result?>">  </p>
          
       <input type="submit" name="submit" value="Calculate"> <tr> </tr>  <tr> </tr> 
       
       <input type="submit" name="clear" value="Clear">

      </form>

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    There are lots of issues here, the biggest is that you are not assigning val1 and val2 from the posted form to the calculator object before calling the add/subtract/etc function.

    Another issue is that you are trying to output the whole object in the result textfield, which will not work and in the php to output the values you need to echo them i.e. <?php echo $value; ?> not just <?php $value; ?>.

    The val1 and val2 of the object need to be public so values can actually be assigned to them. Also because the form is POSTed you can get the values from $_POST instead of $_REQUEST. The last method of the Calculator class also does not make any sense as it tries to use a property of the class that does not even exist. And you can remove the echos from the calls to the add / subtract etc functions as they are redundant.

    The following code should work, see comments for explanations...

    PHP Code:

    // Create class.
    class Calculator 

       public 
    $val1 0;   // All properties public so PHP code can get and set these directly.
       
    public $val2 0;
       public 
    $result 0;  // Results of calculations are stored here and will be numeric.

       
    public function add() 
       { 
          
    $this->result $this->val1 $this->val2;  // Assign result of calculation to result property of this class.
       


       public function 
    subtract() 
       { 
          
    $this->result $this->val1 $this->val2;
       } 

       public function 
    multiply() 
       { 
          
    $this->result $this->val1 $this->val2
       } 

       public function 
    divide() 
       { 
          
    $this->result $this->val1 $this->val2;
       }


    $myCalc = new Calculator();  // Create object called myCalc of the calculator class here so can use in the submit code and also on the form.

    // Check if form was submitted, if so get values to do calculation then call correct method in calculator class.
    if ($_POST['submit'])  
    {
       
    $radios $_POST['radios'];    // Get value of radios so know what to do (add, subtract, etc).       

       
    $myCalc->val1 $_POST['val1'];  // Set value of val1 and val2 of myCalc object to that posted from the form.
       
    $myCalc->val2 $_POST['val2'];
        
       
    // Work out which function to call then call it. No need to echo as it calculates in to the result property of the myCalc object.
       
    if ($radios == "add"
       { 
          
    $myCalc->add(); 
       }
       else if (
    $radios == "subtract"
       { 
          
    $myCalc->subtract(); 
       } 
       else if (
    $radios == "multiply"
       {
          
    $myCalc->multiply(); 
       } 
       else if (
    $radios == "divide"
       { 
          
    $myCalc->divide(); 
       }  

    Then in the HTML you output the result in the result text box by doing echo $myCalc->result. Please remember to close your <br /> tags as well as the inputs with closing slash, also not sure where the four table row <tr> tags came from but they are not needed...

    PHP Code:
    ?>
    <form name="Form1" method="POST" action=""> 
       <p align="left"><strong>Enter the first number :</strong><br /> 
          <input type="text" name="val1" size="20" value="<?php echo $myCalc->val1?>" /><br /> 
       </p> 
       <p align="left"><strong>Enter the second number : </strong><br /> 
          <input type="text" name="val2" size="20" value="<?php echo $myCalc->val2?>" /><br />
       </p>
       <p align="left"> 
          <input type="radio" name="radios" value="add">Addition <br /> 
          <input type="radio" name="radios" value="subtract">Subtraction <br /> 
          <input type="radio" name="radios" value="multiply">Multiplication <br /> 
          <input type="radio" name="radios" value="divide">Division <br /><br /> 
       </p> 
       <p align="left"><strong>Result</strong> 
          <input type="text" name="result" value="<?php echo $myCalc->result?>" />
       </p>     
       <input type="submit" name="submit" value="Calculate">
       <input type="submit" name="clear" value="Clear"> 
    </form>
    <?php
    I guess that's your homework done
    Last edited by DougMck; 10-28-2013 at 02:36 AM.

  • Users who have thanked DougMck for this post:

    Nash Somaroo (10-28-2013)

  • #3
    New Coder
    Join Date
    Oct 2013
    Location
    South Africa
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi DougMck

    Thank you kindly Sir for your help. I have realised that I did not create the $myCalc which was the cause of most of my errors. Also I did not set the value of val1 and val2 of the myCalc object to that posted from the form, and I should have used $_POST instead of $_REQUEST. With regards to the output in the Result text box I was using the echo but it was displaying "$result" in the text box. Its is now just perfect!
    Once again, thank you kindly, Sir.


  •  

    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
    •