开发者

What's a good way to handle post data in Codeigniter?

I.e. would you recomme开发者_开发知识库nd me to use one controller method like this:

function save()
{
    if(!is_bool($this->input->post('')))
    {
        $post_data = $this->input->post('');
        $this->mymodel->save($post_data);
    }
    $this->load->view('myview');
}

Or would you recommend writing it using two methods?

function save()
{
    if(!is_bool($this->input->post('')))
    {
        $post_data = $this->input->post('');
        $this->mymodel->save($post_data);
    }
    redirect('controller/method2')
}

The redirect is the crucial difference here. It prohibits resubmissions from update for example.

How do you do it? Is there another better way?


You should always redirect on a successful form post.


You should always redirect on a successful form post.

Absolutely. For anyone wondering why this is the case, here are a couple of the reasons:

  • Avoid "duplicate submissions". Ever had that when you innocently click refresh or hit the back button and wham, everything has resubmitted?
  • Being friendly to bookmarks. If your user bookmarks the page, presumably you want them to return where they created it, rather than a blank form (a redirect makes them bookmark the confirmation/landing page.

Further reading: http://en.wikipedia.org/wiki/Post/Redirect/Get


As Aren B said, redirection is a good idea, but what I would change in your code is that validation of the post data should be done with the form validation functionallity. It is not only more reauseable but the code will get shorter.

If you want to handle AJAX requests, you would need to return something else than a via or a redirection.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜