Welcome Guest, Not a member yet? Register   Sign In
flashdata not working as expected without redirect()
#1

[eluser]shelton_dev_guy[/eluser]
/** Moved from 'CodeIgniter Discussion' **/

Hi all,

I am developing an application that makes use of session->flashdata() for error/success messages on pages that use forms.

As the admin of the site, I also use the Profiler to track what queries are being executed. When I enabled the profiler, I noticed that some of the queries were not showing up. For example, when a user wants to update their profile, a db->update() function is executed.

After some digging, I realized that when I update the database, I set a flashdata variable and redirect to the same page, and the queries were not being saved to display in the profiler, but the flashdata never fails. When I modify my controller to load->view() instead of redirect(), the profiler shows the query, but now my flashdata is all messed up - it will never show until the next page load (sometimes 2 page loads.)

All I did was comment out the redirects, as shown below.

Controller:
Code:
if ($this->form_validation->run() !== false){
  $upd = $this->users_model->update_user($clean, $user->email);
  if ($upd){
    $this->session->set_flashdata('message', '<p class="success">Your profile has been updated.</p>');
    //redirect('auth/edit_user/'.$user->id);
  } else {
    $this->session->set_flashdata('message', '<p class="fail">Something went wrong, please try again or contact the helpdesk.</p>');        
    //redirect('auth/edit_user/'.$user->id);
  }
}

//this block executes if the form is not submitted, and/or after the validation succeeds/fails.
$data['content'] = "auth/edit_user";
$this->load->view('template', $data, false);

View:
Code:
<h1>Edit Your Profile</h1>
<div class="message">&lt;?php echo $this->session->flashdata('message');?&gt;</div>
//form stuff...

Other information that may be useful:

Config:
Code:
$config['sess_use_database'] = TRUE;

Is there a better (native CI) solution for success/error messages other than using session->flashdata()? I'm not opposed to adding an extra library or a function to one of my custom helpers, but it seems like this can be fixed without a bunch of excess coding.

Thanks,
#2

[eluser]suntzudennis[/eluser]
Hello.

I have the same problem, just does not print or it is unavailable.
My Version 2.1.3
#3

[eluser]CroNiX[/eluser]
Because flashdata sets a cookie, which isn't available until the NEXT page load (just like all cookies).

You can't set and immediately read a cookie without a new request to the server in between. Just the way the web works and nothing new.

http://php.net/manual/en/function.setcookie.php
Quote:Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.
#4

[eluser]suntzudennis[/eluser]
Hi CroNiX

I take the time to test each version from the version:

CodeIgniter V 2.1.3 (Current version)
CodeIgniter V 2.1.2
CodeIgniter V 2.1.1
CodeIgniter V 2.1.0
CodeIgniter V 2.0.3

All this one no print the flashdata.

with the same code works perfectly in this version CodeIgniter_2.0.2
Only in this version works perfectly flashdata.

This is my code:

$this->session->set_flashdata('loginerror','Error en el logeo');

and print this:

echo $this->session->flashdata('loginerror');

Why is that? I have no idea.
#5

[eluser]suntzudennis[/eluser]
Sorry I'm wrong. It works perfectly in this version: CodeIgniter_2.0.2




Theme © iAndrew 2016 - Forum software by © MyBB