Codeigniter表单验证 - 如何成功后取消设置表单值? [英] Codeigniter Form Validation - how to unset form values after success?
问题描述
我意识到这个请求违反了CI文档中提供的示例(建议单独的成功页面视图),但是我想在表单成功提交后重新利用给定的表单视图 - 显示成功消息然后显示空白表单。我尝试了几种方法未成功清除验证集值(取消设置 $ _ POST
,将规则/字段设置为空数组并重新运行验证)。
I realise this request goes against the example provided in the CI documentation (which advises a separate 'success' page view), but I would like to reutilise a given form view after a form has been successfully submitted - displaying a success message then displaying a blank form. I've tried a few ways unsuccessfully to clear the validation set values (unsetting $_POST
, setting rules / fields to an empty array and rerunning validation).
我可以重定向到同一个页面,但是我必须设置一个会话变量来显示一个成功消息 - 这是一个凌乱的方法。
I could redirect to the same page, but then I'd have to set a session variable to display a success message - which is a messy approach.
任何想法如何最好地实现上述目标?
Any ideas how to best achieve the above?
推荐答案
这样,没有提交...。这也提供了一种显示flash_data的方法。
Redirect to itself. This way, no submissions have been run... This also gives you a way to show the flash_data.
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('surname', 'Sur Name', 'required');
if ($this->form_validation->run() === TRUE)
{
// save data
$this->session->set_flashdata('message', 'New Contact has been added');
redirect(current_url());
}
$this->load->view('contacts/add', $this->data);
这篇关于Codeigniter表单验证 - 如何成功后取消设置表单值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!