Pradeep G
Pradeep G

Reputation: 139

codeigniter flashdata not clearing

I'm a newbie to codeigniter and I'm creating a project in which users are created and managed. here I'm using flashdata to display the temporary messages like "user created",etc.,

My code to set flash data is

       $this->session->set_flashdata('message', 'User Created.'); 

In my view I called it as

$this->session->flashdata('message'); 

My problem is that when the user is created,flashdata is displayed and when i click home link the flash data is still available but when i click refresh/home again it disappears. I want it to be cleared when i click the home link for the first time itself. Is there a way to code it??.

Upvotes: 5

Views: 16209

Answers (7)

Aldo
Aldo

Reputation: 923

If nothing else helps, just extend the Session library and add a clear_flashdata function.

<?php defined('BASEPATH') or exit('No direct script access allowed');

// application/libraries/Session/MY_Session.php
class MY_Session extends CI_Session
{

    public function __construct(array $params = array())
    {
        parent::__construct($params);
    }


    /**
     * Clear flashdata
     *
     * Legacy CI_Session compatibility method
     *
     * @param   mixed   $data   Session data key or an associative array
     * @return  void
     */
    public function clear_flashdata($data)
    {
        $this->set_userdata($data, null);
    }
}

Upvotes: 0

Sandeep Sherpur
Sandeep Sherpur

Reputation: 2802

if you want to clear set_flash in controller or another view file, then you can use this simple code.

$this->session->set_flashdata('error', 'User not found...'); //create set_flash

unset set_flash

//echo "<pre>"; print_r($_SESSION); die; //for check 

if(isset($_SESSION['error'])){
    unset($_SESSION['error']);
}

Upvotes: 3

user1133648
user1133648

Reputation:

You can use a Ajax framework for automatically hide the flash message.Also their contains all of the flash operation.

You can get more information from here. https://github.com/EllisLab/CodeIgniter/wiki/Ajax-Framework-For-CodeIgniter

Upvotes: 1

Wasim A.
Wasim A.

Reputation: 9890

You must redirect the page somewhere after $this->session->set_flash('item','value');

Example:

if ($this->form_validation->run() == FALSE){
    $this->session->set_flashdata('error',validation_errors());
    redirect(base_url().'user/login');
}
else{
    $this->session->set_flashdata('success','Thank you');
    redirect(base_url().'user/login');
}

Usually developer make a mistake when they submit data to same page. They set flash data but forget to redirect.

Upvotes: 1

Pupil
Pupil

Reputation: 23958

The flashdata is supposed to display once.

And it gets disappears on page refresh.

So, if you redirect the page to another, it should work.

If you do not refresh the page, you can do it through jQuery.

Say your div displaying flash:

<div id="flash-messages">Success Message</div>

Write jQuery:

<script type="text/javascript">
$(function(){
  $("#flash-messages").click(function(){$(this).hide()});
});
</script>

Upvotes: 2

W Kristianto
W Kristianto

Reputation: 9303

Flashdata will only be available for the next server request, and are then automatically cleared.

if($user_created)
{
    $this->session->set_flashdata('success', 'User created!');
    redirect('login');
}
else
{
    redirect('register');
}

Upvotes: 5

Rohan Kumar
Rohan Kumar

Reputation: 40639

You should redirect after user created. Then when next time you click on home link it will not appear, try this,

$this->session->set_flashdata('message', 'User Created.'); 
redirect(base_url().'home.php');// you can change accordingly

Upvotes: 1

Related Questions