Welcome Guest, Not a member yet? Register   Sign In
Flashdata not being returned properly problem!
#1

[eluser]johnwbaxter[/eluser]
I've got a simple login form with the normal validation and error message areas like you'd expect.

But something odd is going on with retrieving flashdata.

Below is my controller code

Code:
$rules['username']    = "required|min_length[4]|max_length[32]|valid_email";
        $rules['password']    = "required|min_length[4]|max_length[32]|alpha_dash";        

        $this->validation->set_rules($rules);

        $fields['username'] = 'Username';
        $fields['password'] = 'Password';
        
        $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('<p class="error">', '</p>');                
        if ($this->validation->run() == false) {
            
            $flashdata = array('error' => true, 'error_text' => $this->validation->error_string);
            $this->session->set_flashdata($flashdata);
            $this->session->set_flashdata($_POST);
            
            $this->load->view('login_view', $data);
            
        } else {
            //Login to account
            if($this->simplelogin->login($this->input->post('username'), $this->input->post('password'))) {
                
                
                $flashdata = array('success' => true, 'success_text' => 'Login Successful!');
                $this->session->set_flashdata($flashdata);
                
                redirect('');    
            } else {
                
                
                $flashdata = array('error' => true, 'error_text' => 'There was a problem logging into the account.');
                $this->session->set_flashdata($flashdata);
                //$this->session->set_flashdata($_POST);
                
                $this->load->view('login_view', $data);            
            }            
        }

And this is how i show the error in my view:

Code:
&lt;?php echo $this->session->flashdata('error_text');?&gt;

And in my session record i have this:

Code:
a:4:{s:15:"flash:old:error";b:1;s:20:"flash:old:error_text";s:0:"";s:15:"flash:new:error";b:1;s:20:"flash:new:error_text";s:45:"There was a problem logging into the account.";}

When i put a username with the wrong password in and submit the form it doesn't log me in but it does simply reload the page with no error message displayed. However, the error message is in the sessions table. I don't understand why it is not showing in the view if it is in the sessions table. I know flashdata is working to some degree as if i try and submit the form again it comes back and shows the error.

Is it showing the cached html (browser cache not server i don't have caching on) for some reason?

I literally have no idea.
#2

[eluser]OES[/eluser]
Hi.

Cant see the reason why you are storing flashdata in all your code. Its not like you are redirecting after form fail. But you did miss out on adding the array name on success.

Have a look at the below.

Code:
if ($this->validation->run() == false) {

  $data['error_text'] =   $this->validation->error_string;
  $this->load->view('login_view', $data);
        
} else {
  
  if($this->simplelogin->login($this->input->post('username'), $this->input->post('password')))
  {  
    $flashdata = array('success' => true, 'success_text' => 'Login Successful!');
    $this->session->set_flashdata('success', $flashdata);
    redirect('');
  }else{
    $data['error_text'] = array('error' => true, 'error_text' => 'There was a problem logging into the account.');
    $this->load->view('login_view', $data);  
  }
}

Hope this helps.
#3

[eluser]johnwbaxter[/eluser]
Hmm no that makes no difference. Thanks though.

I should probably mention that the $data being passed to the view contains stuff from the rest of the controller and is not for adding anything from the validation process.
#4

[eluser]hostcord[/eluser]
Flashdata is only available on the next request to the server so if you set it and automatically load your view it won't be available. That is why if you reload your page it is available.

Hope that works.
#5

[eluser]johnwbaxter[/eluser]
Reload my page with a redirect do you mean?
#6

[eluser]hostcord[/eluser]
No, I think you need to pass the error message as part of the data array. I was only telling you why it wasn't working.

I would be careful using redirect(''), I doubt that works correctly.

Why set it in flashdata and not the data array? I could see the success page if you want it on a different page, but not to show an error on the same page.
#7

[eluser]johnwbaxter[/eluser]
Well this is the thing, i have always set my messages in the data array but then thought that this flashdata thingy looked interesting and so i wanted to try it out.

I'm just annoyed that i can't get it to work!
#8

[eluser]johnwbaxter[/eluser]
I've gone back to the old ways, it'd be nice to get flashdata working as i can see a real use for it but i can't see what is going wrong.

Ah well.




Theme © iAndrew 2016 - Forum software by © MyBB