0

我有表单的 php 文件,用户需要输入三个参数宽度和高度以及形状三角形或矩形。在我的第一行 php 代码中,我尝试读取宽度和高度参数,在行末代码 php 中,我尝试读取形状 Trigle 或矩形参数,并根据此参数计算面积。感谢任何治疗。

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

?>
4

1 回答 1

3

您的 html 控件名称与 php 变量名称不同。

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

和你的 php

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

它应该是

<br>width.1 <input type=text name='width'>
<br>height.2 <input type=text name='height'>
于 2012-05-03T09:20:03.353 回答