Skip to content

CodeIgniter send and retrieve data using flash messages ?

I am gonna teach you about CodeIgniter flash messages. CI’s flash sessions functionality  let us to use the data for the next server request.Unlike normal sessions flash data will be destroyed permanently after next request, Therefore these can be very useful, and are typically used for showing informational or status messages for example we can use for showing success messages , error messages.. etc.

Note: Flash variables are prefaced with “flash_” so avoid this prefix in your own session names.

How to use

To use flash session in CI, we must configure session key in application/config/config.php file and we must load session library by using auto load mechanism or we can load it in controller or in action.

To add flash data :

$this->session->set_flashdata('item', 'value'); 
// here value can be array

To read or get the data from flash data:

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

If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() function.

$this->session->keep_flashdata('item');

 

Show me example:

For example if user redirects to index action(page) after adding, updating and deleting a record we gonna show message to the user.

Open your controller just add the line before redirect .

<?php 
class Test extends CI_Controller {
   
  public function index() {
    $this->load->view('test_view');
  }
  
  // add record to db
  public function add() {
     // do your logics here 
     $this->session->set_flashdata('item', array('message' => 'Record created successfully','class' => 'success'));
     $this->redirect('/test/index'); 
  }

  // edit record 
  public function edit() {
   // do your logic here 
     $this->session->set_flashdata('item', array('message' => 'Record updated successfully','class' => 'success'));
     $this->redirect('/test/index'); 
  }

  // delete record 
  public function delete($id) {
// do your logic here
     $this->session->set_flashdata('item', array('message' => 'Record updated successfully','class' => 'success'));
     $this->redirect('/test/index'); 
  }

}

you can check and set failure messages also something like

if(true) { 
   $this->session->set_flashdata('item', array('message' => 'Record updated successfully','class' => 'success'));
 
} else {
  $this->session->set_flashdata('item', array('message' => 'please try again!','class' => 'error'));
}

now open your view file (Example : test_view.php). Write code someting like shown below

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

your html here

Hey , we finally knew how to use flash data in CI application. Happy coding …if you get any issues and if i missed anything here let me know via comments i will update the post.Thanks.

0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments