digdigdoot
digdigdoot

Reputation: 761

flashdata being cleared after redirect

For some reason, the flashdata is cleared just before the view is loaded. As the log shows below.

[04/02/2021 13:45:04] INFO: START: result $item {"userdata":{"__ci_last_regenerate":1612417338,"updateeventsuccess":"Event updated successfully","__ci_vars":{"updateeventsuccess":"old"}}}
[04/02/2021 13:45:04] INFO: START: result $item {"userdata":{"__ci_last_regenerate":1612417338}}

How my program flows is that from the initial page, which I will call 'home', I click a link which brings me to an edit page, called 'form', where data fields are found. After filling out the data fields, I will submit the data. In the controller, flashdata is used to store a message about the submission of the data. Then redirect() is used to redirect back to 'home' via its controller method.

Form code where ajax happens:

webix.ajax().post(
   '<?php echo base_url() . 'index.php/Administration/EventController/SaveOrUpdate';?>', 
   form.getValues(), 
   function(){
     location.href = "<?php echo base_url() . 'index.php/Administration/EventController/Listing';?>"
});

The controller method where redirect() is used:

public function SaveOrUpdate()
    {
        $this->RequireRole('admin');
        $this->load->model('Event');
        //get form post data
        $event_form_data = $this->ProcessPostData();
    
        //get post assigned roles
        $post_roles = $this->input->post('roles');
    
        //check if is add or edit
        if($event_form_data->Id <= 0)
        {
            //if add, insert race
            $inserted_id = $this->Event->Insert($event_form_data);
    
            //if successfully inserted/not
            if($inserted_id!=null){
                $this->session->set_flashdata('inserteventsuccess', 'Event inserted successfully');
            }else{
                $this->session->set_flashdata('inserteventfail', 'Failed to insert the event');
            }
        } 
        else
        {
            //if update, update race
            $updated = $this->Event->Update($event_form_data->Id, $event_form_data);
            
            // if successfully updated/not
            if($updated){
                $this->session->set_flashdata('updateeventsuccess', 'Event updated successfully');
            }else{
                $this->session->set_flashdata('updateeventfail', 'Failed to update the event');
            }
        }
        //redirect('/Administration/EventController/Listing'); EDIT Stackoverflow 04/02/2021: Remove this to solve bug.
    }

Finally, the controller method for 'home' where view is loaded:

function Listing() {
        $this->RequireRole('admin');
        $this->load->model('Prospect');
        $this->load->model('Auth/User');
        $this->load->model('Event');
        
        //get user Id(email) and user Name
        $user_email = $this->GetUserId();
        $user = $this->User->GetByUserId($user_email);
        $data['user'] = $user_email;
        $data['username'] = $user->Name;
        $data['users'] = $this->User->GetAll(); 
        $data['events'] = $this->Event->GetAll();
                
        //check whether user is admin/manager
        $user_roles = $this->GetUserRoles();
        $data['admin'] = false;
        $data['manager'] = false;
        foreach($user_roles as $key){
            if($key=='admin'){
                $data['admin'] = true;
            }
            if($key=='manager'){
                $data['manager'] = true;
            }
        }
        // $this->session->keep_flashdata('updateeventsuccess');

        $message = 'START: result $item '.json_encode($this->session);
        $timestamp = date('d/m/Y H:i:s');
        $log_file = 'EPAerror_script.log';
        error_log('['.$timestamp.'] INFO: '.$message.PHP_EOL, 3, $log_file);

        //load dialect listing page
        $data['title'] = 'Event Listing Page';
        $this->load->view('templates/header',$data);
        $this->load->view('templates/navbar',$data);
        $this->load->view('Event/Listing',$data);
        $this->load->view('templates/footer');
    }

Some solutions I have tried is to use keep_flashdata() but it ends up being stored even after refresh and is not cleared from flashdata. This can be seen in the log below:

[04/02/2021 13:59:09] INFO: START: result $item {"userdata":{"__ci_last_regenerate":1612418277,"updateeventsuccess":"Event updated successfully","__ci_vars":{"updateeventsuccess":"new"}}}
[04/02/2021 13:59:11] INFO: START: result $item {"userdata":{"__ci_last_regenerate":1612418277,"updateeventsuccess":"Event updated successfully","__ci_vars":{"updateeventsuccess":"new"}}}

Any advice on how to work around this?

Upvotes: 0

Views: 70

Answers (1)

digdigdoot
digdigdoot

Reputation: 761

The redirect is being called twice. First in the SaveOrUpdate method and second in the ajax success function.

The bug was solved by removing the redirect() call in the SaveOrUpdate method.

Upvotes: 0

Related Questions