0

我正在 CI 中建立一个基本网站。我已经使用表单助手构建了一个表单,并且正在按照以下方式做一些事情:

查看(来自 create_form.php):

<?php $this->load->helper('form'); ?>
<?php echo form_open('site/create_article'); ?>
<?php echo form_label('Title:', 'title'); ?><br />
<?php echo form_input('title'); ?><br /><br />
<?php echo form_label('Body:', 'text'); ?><br />
<?php echo form_textarea('text'); ?><br /><br />
<?php echo form_submit('submit', 'Post Article'); ?>

控制器(来自 site.php):

function create_article()
{
    $this->load->model('site_model');
    $this->load->helper('form');

    $post_check = $this->input->post('submit');

    if ($post_check === TRUE)
    {
        $this->site_model->create_article($post_check);
        $this->load->view('created');
    }
    else
    {
        $this->load->view('create_form');
    }
}

模型:

function create_article($post_check)
{
    $this->load->helper('date');

    $data = array(
                  'title' => $post_check['title'],
                  'text' => $post_check['text'],
                  'created' => now()
                 );

    $this->db->insert('articles', $data);
}

当我提交表单时,它只是重新加载“create_article.php”(包含表单)而不是确认页面“created.php”。大概 $post_check 没有收到任何传递给它的数据,但我不确定为什么刷新页面后提交会触发 POST 数据通知 - 肯定会发生一些事情!欢迎任何建议。

4

2 回答 2

0
$post_check = $this->input->post('submit'); // THIS return array()

$post_check === TRUE; // THIS IS INVALID !!! array() !== TRUE !!

// You should do 

if ( count($post_check) ){ }
于 2013-10-06T13:56:44.527 回答
0
 form_submit('submit', 'Post Article');
// Would produce:

 <input type="submit" name="submit" value="Post Article" />


 $post_check = $this->input->post('submit');


 if ($post_check == 'Post Article')
  {
    $this->site_model->create_article($post_check);
    $this->load->view('created');
  }
   else
  {
    $this->load->view('create_form');
   }
于 2013-10-06T16:04:54.383 回答