|
新手:刚学CI一个星期,还望各位大牛帮忙解答下疑惑,感激感激~~
问题1:
控制器中的create()方法在执行规则判断时候,视图创建表单的页面都还没有被加载,怎么就直接先使用表单中就是下面的这段代码,
表单的tille和text都没有被提交怎么就先进行判断是否为空,然后判断完了才加载视图的这个表单
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
问题2:我感觉CI这些表单验证函数,是不是直接使用PHP的原生功能更方面点,
比如view下面使用PHP原生的语法创建form表单,model下面进行form数据接收,就是还是使用MVC模型,但是里面的语法基本还是PHP原生的
新手可能刚学,感觉不到CI的方便之处
问题3:我成功提交了表单后数据没有提交,所以success也米有加载。页面显示http 404无法访问
-----------------------------------------------------------------------------------------
控制器:
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function create()
{
$this->load->helper('form_helper');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
-------------------------------------------------------------
视图
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
--------------------------------------------------------------
模型
<?php
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
}
?>
|
|