开发者

Codeigniter - handle form submission from different controller action

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.

Whats the best way to do this - w开发者_JAVA技巧ithout having the index controller handle the main page loading and the form submission.


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');
}

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.

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());


Simply point your form at your submit action:

<form method="post" action="/home/submit">

Then, use the submit action to validate your input before redirecting back to the index action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜