我无法使用post成功地将参数从表单传递到php代码

时间:2012-05-03 09:17:10

标签: php html forms

我有php文件的形式,用户需要输入三个参数的宽度和高度和形状Tringle或矩形。 在我的第一行PHP代码中我尝试读取宽度和高度参数,并在行代码php的末尾我尝试读取形状Tringle或矩形参数并根据此参数计算区域。 对任何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 个答案:

答案 0 :(得分: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'>