1

Possible Duplicate:
How to access a variable in a PHP class that is set by another function? Codeigniter

I have a controller called submit:

Basically it does two things of concern:

  1. validate_form() - It validates a form -> then submits to database.
  2. success() - It loads a page giving feedback on whether the previous operation was successfull.

My controller works fine however there is a slight problem in when it loads my success page. If you hit Refresh the "success" page it would for some reason cause my validate_form() function to run again causing the same data to be duplicated in the database.

What I want is that when the user goes to success page they can hit refresh as many times they like and it wont call any functions etc.

My url on the success view is

http://localhost/site/validate_form

I am guessing this is the problem because it is still in the validate_form() function.

My validate form function loads the success view by calling another function within it? Here is my code:

//Record ID = returned value
                        $record_id = $this->submit_model->create_record($completedstaffrows, $completedeventrows);

                        //if no value returned call failed function

                        if ($record_id == FALSE)
                        {
                            $this->failed();
                        }

                        //Submittal to database was successful
                        //Now call function to load success view
                        else
                        {

                            $this->success();
                        }

My function for loading success view:

    public function success()
        {
            $page['page'] = 'success';

            $this->load->view('template', $page );

        }
Community
  • 1
  • 1
sqlmole
  • 997
  • 7
  • 17
  • 31

1 Answers1

1

What you need is a Post/Redirect/Get pattern. After the form is submitted, make a 301 redirect to the same page, maybe with a session variable to know whether the data was valid or not, and this would prevent any duplicate data issue on refresh.

Eduardo Reveles
  • 2,155
  • 17
  • 14
  • Ok I tried a redirect however I now have a problem that my variable that I was passing the original way is not getting passed to the view and is causing an error (undefined). How can I pass a variable using redirect? – sqlmole Jul 30 '11 at 17:27
  • You can use [CI sessions](http://codeigniter.com/user_guide/libraries/sessions.html) to store/retrieve values between requests. Take a look at the flashdata section, it would do the work you need. – Eduardo Reveles Jul 30 '11 at 20:05