0

I have the following method in my controller:

(shortened version but all the key pieces are here...)

class Widget extends CI_Controller {
        public function __construct()
        {
                parent::__construct();
                $this->load->model('widget_model');
                $this->load->helper('form');
                $this->load->library('form_validation');
        }

        public function assign()
        {
                //logic to display form. 
                if ($this->input->method() == "get" ) {
                        $data['save_status'] = '';
                        $data['title'] = 'Assign Widget';
                        $data['main_content'] = "assign";
                        $this->load->view('includes/template',$data);
                } 
                else 
                {
                       //logic to handle POST
                        ...
                        $data['save_status'] = 'Sucessfully saved to db';
                        $data['title'] = 'Assign Widget';
                        $data['main_content'] = "assign";
                        $this->load->view('includes/template',$data);
                }
           }

Everything works, except I don't know what the proper way to clear the form data is... because when I press F5 thinking I'm refreshing my page, it's actually resubmitting the data to the database.

Sorry, i'm sure this is a noob question.

Thanks.

EDIT 1

for now, I added a redirect at the end of post logic like this;

  redirect(base_url()."index.php/thesamecontroller/thesamemethod");

and that takes the user to the same page, but without the form data being available.

Is this the best way to handle it?

Happydevdays
  • 1,982
  • 5
  • 31
  • 57
  • Redirect to method after the form submits, or use ajax to submit the form. – Kisaragi Nov 10 '16 at 00:20
  • @Kisaragi ha! we must have been adding comments at the same time. please see my EDIT 1 – Happydevdays Nov 10 '16 at 00:21
  • If you wan't to keep the form data, you'll need to either retrieve it from the db or pass it to the redirect. – Kisaragi Nov 10 '16 at 00:26
  • Possible duplicate of [how to prevent form resubmission when page is refreshed via PHP](http://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-via-php) – Tpojka Nov 10 '16 at 00:52

0 Answers0