Codeigniter – Post resubmit problem on refresh

A small tip for codeigniter programmers.

code-igniter-logo

If you use same controller for display view page and take action on POST, there is a problem may happen when you try to refresh the page after submit, the whole data get re-submit and creates problem.

It can be solved using session. If there is any POST form submit,

ie

if (count($_POST) > 0)

{

$this->session->set_userdata('post_data', $_POST );
redirect('same_controller');

}

else

{

if($this->session->userdata('post_data'))
{

$_POST = $this->session->userdata('post_data');

$this->session->unset_userdata('post_data');
}

}

This will solve the problem of resubmit !!!

Or simply you can use another controller for POST method and separate the form display using another controller (Normal way)

3 Comments

3 Responses to “Codeigniter – Post resubmit problem on refresh”

  1. harman February 20, 2010 at 1:22 pm #

    No need to use another controller for POST method.
    USE same controller with some method like..

    ———–your_controller———–
    index(){
    //YOUR controller DATA
    }
    save(){
    //GET YOUR POST DATA HERE
    //SAVE YOUR POST DATA
    redirect(‘your_controller’);
    }

  2. D Whatley July 12, 2010 at 11:15 am #

    Thank you! I struggled with what to do about this for well over an hour.

  3. Bie October 17, 2011 at 10:56 pm #

    Thank you. You save of my life :)

Leave a Reply

More in php (40 of 101 articles)


From PHP we usually do a lot of coding to create a webservice client. We use nusoap library for this ...