Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I.e. would you recommend 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?

like image 384
C. E. Avatar asked Nov 28 '22 11:11

C. E.


2 Answers

You should always redirect on a successful form post.

like image 164
Aren Avatar answered Dec 18 '22 08:12

Aren


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

like image 20
wally Avatar answered Dec 18 '22 08:12

wally