0

I have php file with form which the user need to enter three parameters width and height and shape Tringle or rectangle. in my first line code of php I try to read the width and height parameters and in the end of lines code php I try to read the shape Tringle or rectangle parameter and according this parameters to calculated the area. Thx for any healp.

 <h2>OOP Class Demo</h2>
<p>
Please enter two numbers to calculated the area of rectangle or tringle and press submit...
<p>
<form method="post" action="demo.php">
<br>width.1 <input type=text name=num_a>
<br>height.2 <input type=text name=num_b>
<br><input type="radio" name="shape" value="Rectangle" /> Rectangle
<br><input type="radio" name="shape" value="Tringle" /> Tringle
<br><input type=submit>
</form>

<?php

  echo("<br>");
  if(isset($_POST['submit']))
   {echo "THERE is submit.!"; 
    Class Rectangle {

    //Declare the attributes:
    public $width = $_POST['width'];
    public $height = $_POST['height'];
    protected static $formula = 0;

//Method to set the dimensions.
    Function create_formula() {
        self :: $formula = $this->width * $this->height;
    }

    //Method to set the dimensions.
    Function set_size($w = 0, $h = 0) {
            $this->width = $w;
            $this->height = $h;
            $this->create_formula();
    }

    //Method to calculate and return the area.
    function get_area() {
            return (self::$formula);
            }
    }

    Class Triangle extends Rectangle{
        Function create_formula() {
            self :: $formula = ($this->width * $this->height)/2;
        }
    }

    if (!$_POST['shape']){echo('your choice is: '.$_POST['shape']);}
    // create object of rectangle and calculate it is area
    $rect = new Rectangle ();
    echo ($rect->get_area()."<br />");

    // create object of tringle and calculate it is area by extends
    $tri = new Triangle ();
    echo $tri->get_area();
    }

?>

1 Answer 1

3

Your html control name is different than php variable names.

<br>width.1 <input type=text name=num_a>
<br>height.2 <input type=text name=num_b>

and your php

public $width = $_POST['width'];
public $height = $_POST['height'];

It should be

<br>width.1 <input type=text name='width'>
<br>height.2 <input type=text name='height'>
Sign up to request clarification or add additional context in comments.

1 Comment

after that i fixed i got error :Multiple annotations found at this line: - syntax error, unfinished class declaration - syntax error, unexpected '$_POST' thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.