Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$this->session->set_flashdata() and then $this->session->flashdata() doesn't work in codeigniter

Please take a look at the following code:

$this->session->set_flashdata('message', 'This is a message.');
var_dump($this->session->flashdata('message'));

It returns bool(false).

I hope it doesn't NEED a redirect() to be recallable after that, because some times I need to call it right after setting the flashdata in a view.


Edit after venkat's comment

$this->session->set_flashdata('message', 'This is a message.');
$this->session->keep_flashdata('message');
var_dump($this->session->flashdata('message'));

No difference unfortunately.

like image 716
Mohammad Naji Avatar asked Oct 28 '12 14:10

Mohammad Naji


People also ask

How to set flashdata in CodeIgniter?

flashdata() function makes sure that you are getting only flash data and not any other data. $this->session->flashdata('item'); If you do not pass any argument, then you can get an array with the same function.

What is Flashdata in CI?

Flashdata. CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared. This can be very useful, especially for one-time informational, error or status messages (for example: “Record 2 deleted”).

How does CodeIgniter session work?

Sessions data are available globally through the site but to use those data we first need to initialize the session. We can do that by executing the following line in constructor. $this->load->library('session'); After loading the session library, you can simply use the session object as shown below.

What is Flashdata?

Flashdata is a functionality in the CodeIgniter framework that lets you make required data available for the next server request. It makes use of session, but unlike normal session behavior, the passed data is available only for the next server request and is automatically cleared thereafter.


4 Answers

Well, the documentation does actually state that

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.

as the very first thing, which obviusly means that you need to do a new server request. A redirect, a refresh, a link or some other mean to send the user to the next request.

Why use flashdata if you are using it in the same request, anyway? You'd might as well not use flashdata or use a regular session.

like image 71
Repox Avatar answered Oct 21 '22 04:10

Repox


// Set flash data 
$this->session->set_flashdata('message_name', 'This is my message');
// After that you need to used redirect function instead of load view such as 
redirect("admin/signup");

// Get Flash data on view 
$this->session->flashdata('message_name');
like image 36
Omprakash Patel Avatar answered Oct 21 '22 02:10

Omprakash Patel


To set flashdata you need to redirect controller function

$this->session->set_flashdata('message_name', 'This is test message');

//redirect to some function
redirect("controller/function_name");

//echo in view or controller
$this->session->flashdata('message_name');
like image 12
prash.patil Avatar answered Oct 21 '22 02:10

prash.patil


Displaying a flash message after redirect in Codeigniter

In Your Controller set this

<?php

public function change_password(){







if($this->input->post('submit')){
$change = $this->common_register->change_password();

if($change == true){
$messge = array('message' => 'Password chnage successfully','class' => 'alert alert-success fade in');
$this->session->set_flashdata('item', $messge);
}else{
$messge = array('message' => 'Wrong password enter','class' => 'alert alert-danger fade in');
$this->session->set_flashdata('item',$messge );
}
$this->session->keep_flashdata('item',$messge);



redirect('controllername/methodname','refresh');
}

?>

In Your View File Set this
<script type="application/javascript">
/** After windod Load */
$(window).bind("load", function() {
  window.setTimeout(function() {
    $(".alert").fadeTo(500, 0).slideUp(500, function(){
        $(this).remove();
    });
}, 4000);
});
</script>

<?php

if($this->session->flashdata('item')) {
$message = $this->session->flashdata('item');
?>
<div class="<?php echo $message['class'] ?>"><?php echo $message['message']; ?>

</div>
<?php
}

?>

Please check below link for Displaying a flash message after redirect in Codeigniter

like image 8
Jydipsinh Parmar Avatar answered Oct 21 '22 02:10

Jydipsinh Parmar