Reputation: 457
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();
}
?>
Upvotes: 0
Views: 209
Reputation: 2640
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'>
Upvotes: 3