我刚刚开始学习CodeIgniter,遇到了下面的例子:我的控制器:<>之前类Site扩展CI_Controller{公共函数__construct(){
父:__construct ();}函数索引(){$ this ->加载>视图("options_view");}创建()函数{$data = array ('title' => $this->load->input->post('title'),'content' => $this->load->input->post('content'));$ this -> site_model -> add_record(元数据);$ this ->索引();}}之前我的模型是:<>之前类Site_model扩展CI_Model{get_records()函数{$q = $this->db->get('articles');返回$ q ->结果();}函数add_record(元数据){$ this -> db -> insert("文章",元数据);返回;}}
我的观点是:
<pre>
<?php echo form_open('site/create');?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title"/>
</p>
<p>
<label for="content">Content:</label>
<input type="text" name="content" id="content"/>
</p>
<p>
<input type="submit" value="Submit"/>
</p>
<?php echo form_close();?>
</pre>
所以当我点击Submit按钮时,我得到的错误如下:
Severity: Notice
Message: Undefined property: CI_Loader::$input
Filename: controllers/site.php
Line Number: 19
任何想法都会有用的!谢谢!
在你的控制器上试试。
class Site extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('options_view');
}
function create()
{
$data = array (
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->index();
}
}
不需要在create函数的输入语句前面使用load。试一试. .
这些行应该像
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
不'title' => $this->load->input->post('title'),
'content' => $this->load->input->post('content')
,还需要加载表单帮助器。所以在parent::__construct()之后;添加这一行或者您可以添加到您的autoload.php页面
$this->load->helper('form');
如果你有任何问题请告诉我。