2014-01-07 42 views
2

我已经在html中创建了一个表单,它使用php中的post方法将所有信息打印出来供用户使用,但它看起来像我的表单有问题。它不打印用户输入的详细信息,我试图填充,然后按提交创建类配置文件的对象,将用户的所有信息分配给对象并在浏览器上打印出来,但没有任何内容出现在浏览器上。表单不打印出详细信息

我也试图回应像getFirstName这样的方法,但它同样没有出现,任何人都可以帮助我找出最新的代码问题,我该如何解决它。

请注意,我已经包含三个不同的文件,一个是html表单,另一个是passingdata.php,它将获取用户输入的所有信息,第三个文件是用于传递数据以创建一个对象并为其提供所有需要的信息以创建该类的对象。

最后,我已经调用了哪些应打印出用户输入的所有信息的方法printDeatils

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title><?php echo 'Student Deatils';  ?></title> 
    </head> 
    <body> 
     <p>Please enter your Details:</p> 
     <div> 
     <form name="student" method="post" action="passingdata.php"> 
      <div> 
      <label>First name:</label> <input type ="text" name="first_name"> 
      <label>Last name</label> <input type ="text" name="last_name"> 
      <br></br> 
      <label>International student</label> <input type="checkbox" name="international"> 
       <br></br> 
       <fieldset> 
        <legend>Course</legend> 
      <label>CS <input type="radio" name="course" value="CS"></label> 
      <label>SE <input type="radio" name="course" value="SE"></label> 
      <label>MIT <input type="radio" name="course" value="MIT"></label> 

      </fieldset> 
       <br></br> 
       <input type="submit" name="Submit" value="Submit"> 
       <input type="reset" name="Resit" value="Reset"> 
      </div> 
     </form> 
      </div> 
     <?php 


     ?> 
    </body> 
</html> 

的传递数据文件:

​​

类简介:

<?php 


class Profile { 
    private $_firstName, $_lastName, $_international, $_course; 

    function __contruct ($firstName, $lastName, $course, $international) { 
$this->_firstName = $firstName; 
$this->_lastName = $lastName; 
$this->_course = $course; 
$this->_international = $international; 
} 



    public function setFirstName($firstName) 
    { 
     $this->_firstName = $firstName; 

    } 
    public function setLastName($lastName) 
    { 
     $this->_lastName = $lastName; 
    } 
    public function setInternational($inter) 
    { 

      $this->_international = $inter; 
    } 
    public function setCourse($course) 
    { 
      $this->_course = $course; 
    } 
    public function getFirstName() 
    { 
     return $this->_firstName; 
    } 
     public function getLastName() 
    { 
      return $this->_lastName; 
    } 
     public function getInternational() 
    { 
     return $this->_international; 
    } 
    public function getCourse() 
    { 

     return $this->_course; 
    } 
    public function printDtails() 
    { 
     echo "$_firstName"; 
    } 
} 

?> 
+0

您需要回显'$ this - > _ firstName' - 您在setters和getters中正确使用它,但不能在打印功能中使用它。 – andrewsi

+0

$ this - > _ firstName不是$ _firstName – 2014-01-07 19:55:55

+0

我试过了,但它不起作用 – user3170512

回答

0

printDtails()功能,你需要echo $this->_firstName

而且在你的班上,字结构的拼写错误,你有__contruct它需要__construct

+0

我已经尝试过,但它不工作 – user3170512

+1

你的类构造拼写错误,你有“__结构”它需要是“__结构” – frosty11x

0

这为我工作,但我也稍微修改代码。

我必须承认自己正在学习课程,所以这可能是也可能不是你想要的,但它确实有效。

此外,您printDtails()函数中,它需要一个echo $this->_firstName;

我改变了你的结构到$this->_firstName = $_POST['first_name'];

注:construct是拼写错误为contruct与失踪s

这是我想出的:

<?php 

class Profile { 

    private $_firstName, $_lastName, $_international, $_course; 
    function __construct ($_firstName, $_lastName, $_international, $_course) 

{ 
$this->_firstName = $_POST['first_name']; // modified 
$this->_lastName = $_POST['last_name']; // modified 
$this->_course = $_POST['course']; // modified 
$this->_international = $_POST['international']; // modified 
} 

    public function setFirstName($firstName) 
    { 
     $this->_firstName = $firstName; 
    } 

    public function setLastName($lastName) 
    { 
     $this->_lastName = $lastName; 
    } 
    public function setInternational($inter) 
    { 
      $this->_international = $inter; 
    } 
    public function setCourse($course) 
    { 
      $this->_course = $course; 
    } 
    public function getFirstName() 
    { 
     return $this->_firstName; 
    } 
     public function getLastName() 
    { 
      return $this->_lastName; 
    } 
     public function getInternational() 
    { 
     return $this->_international; 
    } 
    public function getCourse() 
    { 
     return $this->_course; 
    } 
    public function printDtails() 
    { 
    echo $this->_firstName . "\n"; // modified 
    echo $this->_lastName . "\n"; // modified 
    echo $this->_course . "\n"; // modified 
    echo $this->_international . "\n"; // modified 
    } 
} 

?>