即使在成功之后,Codeigniter表单字段也会使用先前的数据重新填充

时间:2014-10-14 15:10:34

标签: php forms codeigniter

当表单数据不正确或数据不足时,它会显示验证错误以及填充过去数据的表单字段。但即使from完成并正确提交,它也会在表单字段中显示提交的值。如何解决问题?

我正在寻找评论表。这是我的控制器功能:

    public function index($id, $slug){
        // Fetch the article
        $this->article_m->set_published();
        $this->data['article'] = $this->article_m->get($id);        
        // Return 404 if not found
        count($this->data['article']) || show_404(uri_string());        
        // Redirect if slug was incorrect
        $requested_slug = $this->uri->segment(3);
        $set_slug = $this->data['article']->slug;
        if ($requested_slug != $set_slug) {
            redirect('article/' . $this->data['article']->id . '/' . $this->data['article']->slug, 'location', '301');
        }
$c_rules = $this->article_comment_m->c_rules; 
$this->form_validation->set_rules($c_rules);

        // Process the form
        if ($this->form_validation->run() == TRUE) {

            $data = array(
                'article_id'=>$this->input->post('article_id'),

                'name'=>$this->input->post('name'), 
                'email'=>$this->input->post('email'), 
           'website'=>$this->input->post('website'), 

              'comment'=>$this->input->post('comment'),
              'posted'=>date('Y-m-d')
            );

            $this->article_comment_m->save_comment($data);


        }

        // Load view
add_meta_title($this->data['article']->title);
        $this->data['subview'] = 'article';
        $this->load->view('_main_layout', $this->data);

    }    

我的视图文件包含以下数据:

     <?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<?=form_hidden('article_id', uri_string());?>
<table class="table">
<tr>
    <td>Name:</td>
    <td><?php echo form_input('name', set_value('name')); ?></td>
    </tr>
<tr>
<td>Email:</td>
    <td><?php echo form_input('email', set_value('email')); ?></td>
  </tr>
  <tr>
<td>Website:</td>
    <td><?php echo form_input('website', set_value('website')); ?></td>
  </tr>

<tr>
<td>Comment:</td>
<td><?php echo form_textarea('comment', set_value('comment')); ?></td>
   </tr>

  <tr>

    <td><?php echo form_submit('submit', 'Submit', 'class="btn btn-primary"'); ?></td>
  </tr>
</table>
<?php echo form_close();?>

1 个答案:

答案 0 :(得分:1)

您需要重定向回url以删除所有session data,成功执行此操作并验证true POSTS 优点是,当用户刷新页面时,它不会重新发送FORM