Codeigniter - 处理来自不同控制器操作的表单提交 [英] Codeigniter - handle form submission from different controller action
问题描述
在codeigniter中处理以下情况的最佳方法是:
Whats the best way to handle the following situation in codeigniter:
家庭控制器具有索引操作和提交操作。
Home controller has an index action and a submit action.
提交操作用于表单提交。我想通过索引控制器加载页面 - 包括在表单提交后,即与形式错误数据重新填充表单输入错误等。
The submit action is used for a form submission. I want to load the page through the index controller though - including after form submission i.e. with form errors data to repopulate form inputs on error etc.
最好的办法
推荐答案
如果您没有索引控制器处理主页加载和表单提交。 redirect()
,您将无法使用 set_value()
用于重新填充表单字段。
If you redirect()
, you won't be able to use set_value()
for re-populating your form fields.
最简单的方法是使用索引控制器默认加载行为和提交。
What's easiest is having your index controller handle both the default load behavior, and the submission.
function index()
{
if($this->input->post('foo'))
{ // something was POSTed
$this->load->library('form_validation');
//validation rules
} else
{ // normal view
//
}
$this->load->view('home');
}
或者,您可以设置 / code>和
提交
控制器,并让他们指向同一个视图,它检测 validation_errors >设置并重新填充表单字段。
Alternatively, you can just set up your index
and submit
controller, and have them point at the same view, which detects if validation_errors()
are set and re-populates form fields accordingly.
第三个选项(hackish):你可以使用flashdata来保持提交错误,指数。类似这样的工作方式:
Third option (hackish): you could use flashdata to keep submission errors and the submitted form values across a redirect back to index. Something like this would work:
$this->session->set_flashdata('errors', $validation_errors());
这篇关于Codeigniter - 处理来自不同控制器操作的表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!