在对象的方法中进行表单渲染

时间:2013-07-04 03:40:33

标签: php forms templates

我是PHP MVC方法的新手,最近我遇到了从View.php显示表单的问题

代码在这里(Controller_Shipping.php)

<?php
include 'View_Shipping.php';
 class Controller_Shipping{

    public $view;

    public function __construct(){
        $this->view = new View_Shipping();
    }

    public function index(){
        $this->view->showForm();
    }
 }
 ?>

视图文件如下所示(View_Shipping.php)

<?php
class View_Shipping{

    public function showForm(){
?>      
 <form method="post">
    <h3>Shipping Information</h3>
   <p>
  <label for="name"> Title Name </label>
  <input type="text" id="name" name="Title" autofocus>
 </p>
   <p>
<button type = "submit"> Save </button> 

  </p>
</form>
<?php
} // end of showForm()
}
 ?>

当我通过“localhost / Controller_Shipping.php”加载它时,它什么都没显示。但我创建另一个非常简单的PHP,如“localhost / testing.php”,只包含

<?php

include "Controller_Shipping.php";

$testing = new View_Shipping();

echo $testing->showForm();


?>

有效。来到控制器失败..是否与为Controller类分配新对象有关?

1 个答案:

答案 0 :(得分:0)

你没有回应内容(就像你在测试中一样)。 做任何一件事

 public function index(){
       echo $this->view->showForm();
    }

 public function showForm(){

 echo '<form method="post">
    <h3>Shipping Information</h3>
   <p>
  <label for="name"> Title Name </label>
  <input type="text" id="name" name="Title" autofocus>
 </p>
   <p>
<button type = "submit"> Save </button> 

  </p>
</form>';

} // end of showForm()

坦率地说,我不会让View_Shipping成为一个类。只需将其设为标准的php文件

即可
相关问题