Display Flash Message in CodeIgniter - Mostlikers

11 June, 2015

Display Flash Message in CodeIgniter

Let me explain about the CodeIgniter session flash message. it's a temporarily stored value. this coding used to set success message and failure message through your controller. Let start to learn how to create CodeIgniter session flash message.

Display Flash Message in CodeIgniter



The session store data will available temporarily it will work with single server request. Once get the
 next server request session data will clear automatically.

Flash session set code work with your controller functions. Session response code displays your flash view files. Generally, we will success message after submitting the form try to use session message.

Set flash data in controller

message - flash data session name. 
$this->session->set_flashdata('message', 'Sucessfully updated.');

message - It's a session temporary name you can give any name like 'delete_data', 'error_msg'

Read flash data

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

message - set_flashdata('message') name.

Welcome Controller 
Create sample controller class name called to welcome and set your session
<?php 

class Welcome extends CI_Controller {
    
    public function add_user()
    {
        //insert or update code
        $this->session->set_flashdata('message', 'Successfully Added.');
        $this->load->view('index');
    }
    

}

View(index.php)
I have to load the session message index file

<html>
<body>
<?php if($this->session->flashdata('message')){?>
  <div class="alert alert-success">      
    <?php echo $this->session->flashdata('message')?>
  </div>
<?php } ?>
</body>
</html>


Sessflashesata typically used for showing informational or status messages for alert and success message example success alert message,Error message, Info messages. We can set flash lot message in same flash data name.

Thank you for visiting. if have any queries write comments below.

"To help yourself, you must be yourself. Be the best that you can be. 
When you make a mistake, learn from it, pick yourself up and move on.  "
                                             
                                         - Dave Pelzer








Related Topics


8 comments: