2012-05-03 8 views
0

私は、ユーザーが3つのパラメータの幅と高さを入力し、Tringleまたは長方形を入力する必要があるフォームを持つPHPファイルがあります。 PHPの私の最初の行のコードでは、私は、幅と高さのパラメータを読み取るしようとすると、私は、Tringleまたは長方形のパラメータの形状を読み取るしようとすると、このパラメータは、領域を計算します。 ヒープのThx。私は正常にフォームからPHPコードにパラメータを渡すことができません

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

?> 

答えて

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'> 
+0

その後、私はエラーを得た固定する必要があります。このラインで複数見つかりまし注釈: \t - 構文エラー、未完成のクラス を\t宣言 \t - 構文エラー、予期しない '$ _POST'ありがとう –

関連する問題