0

由于某种原因,flashdata 在视图加载之前被清除。如下日志所示。

[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}}

我的程序是如何运行的,从我称之为“主页”的初始页面开始,我单击一个链接,将我带到一个名为“表单”的编辑页面,在该页面中可以找到数据字段。填写完数据字段后,我将提交数据。在控制器中,flashdata 用于存储有关数据提交的消息。然后redirect()用于通过其控制器方法重定向回“家”。

发生ajax的表单代码:

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';?>"
});

使用的控制器方法redirect()

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.
    }

最后,加载视图的“home”的控制器方法:

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');
    }

我尝试过的一些解决方案是使用keep_flashdata(),但即使在刷新后它最终也会被存储,并且不会从 flashdata 中清除。这可以在下面的日志中看到:

[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"}}}

关于如何解决这个问题的任何建议?

4

1 回答 1

0

重定向被调用两次。首先在SaveOrUpdate方法中,其次在 ajax 成功函数中。

通过删除方法redirect()中的调用解决了该错误SaveOrUpdate

于 2021-02-04T06:59:31.547 回答