我正在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'); ?>
Controller(来自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数据通知 - 这肯定会有所收获!欢迎任何建议。
答案 0 :(得分: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) ){ }
答案 1 :(得分: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');
}