ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

flashdata not working as expected without redirect()

September 30, 2010 7:37pm

Subscribe [3]
  • #1 / Sep 30, 2010 7:37pm

    shelton_dev_guy

    6 posts

    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:

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

    <h1>Edit Your Profile</h1>
    <div class="message"><?php echo $this->session->flashdata('message');?></div>
    //form stuff…

    Other information that may be useful:

    Config:

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

    If this post is not in the right area, can a mod please move it?

    Thanks,

  • #2 / Oct 01, 2010 9:37am

    danmontgomery

    1802 posts

    http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

    Flashdata

    CodeIgniter supports “flashdata”, or session data that will only be available for the next server request

  • #3 / Oct 01, 2010 9:55am

    n0xie

    1381 posts

    You could try my Message Library which solves this.

  • #4 / Oct 01, 2010 11:00am

    pickupman

    1317 posts

    What happens when you try

    if ($this->form_validation->run() !== false){
      $upd = $this->users_model->update_user($clean, $user->email);
      if ($upd)
        $this->session->set_flashdata('message', 'Your profile has been updated.');
      else
        $this->session->set_flashdata('message', 'Something went wrong, please try again or contact the helpdesk.');        
       
      redirect('auth/edit_user/'.$user->id, 'refresh'); 
    }
  • #5 / Oct 01, 2010 1:06pm

    shelton_dev_guy

    6 posts

    @noctrum
    So loading a view doesn’t really make another http request?

    @pickupman
    If your solution does not work, I will try n0xie‘s library.

    Thanks for the replies!

  • #6 / Oct 01, 2010 1:45pm

    danmontgomery

    1802 posts

    @noctrum
    So loading a view doesn’t really make another http request?

    No… redirect() makes another http request

  • #7 / Oct 01, 2010 2:57pm

    shelton_dev_guy

    6 posts

    —Edited—

    My question changed, so I will make another post. Thanks for the help—I am going to use n0xie‘s message lib.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases