【发布时间】:2014-10-14 15:10:34
【问题描述】:
当表单的数据不正确或不足时,它会显示验证错误以及填充了过去数据的表单字段。但即使 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();?>
【问题讨论】:
标签: php forms codeigniter