We will learn how to How to implement flash messages in PHP Codeigniter


Step 1: 

Download Fresh Codeigniter 3: Download Codeigniter 3


Step 2: 

Add Route application/config/routes.php

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


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


$route['success-flash'] = 'MyFlashController/success';
$route['error-flash'] = 'MyFlashController/error';
$route['warning-flash'] = 'MyFlashController/warning';
$route['info-flash'] = 'MyFlashController/info';


Step 3: 

Add Controller application/controllers/MyFlashController.php

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


class MyFlashController extends CI_Controller {


  /**
    * Manage __construct
    *
    * @return Response
   */
   public function __construct() { 
      parent::__construct(); 
      $this->load->library("session");
   }


	/**
    * Get All Data from this method.
    *
    * @return Response
   */
	public function success()
	{
      $this->session->set_flashdata('success', 'User Updated successfully');
      return $this->load->view('myPages');
	}


  /**
    * Get All Data from this method.
    *
    * @return Response
   */
  public function error()
  {
      $this->session->set_flashdata('error', 'Something is wrong.');
      return $this->load->view('myPages');
  }


  /**
    * Get All Data from this method.
    *
    * @return Response
   */
  public function warning()
  {
      $this->session->set_flashdata('warning', 'Something is wrong.');
      return $this->load->view('myPages');
  }


  /**
    * Get All Data from this method.
    *
    * @return Response
   */
  public function info()
  {
      $this->session->set_flashdata('info', 'User listed bellow');
      return $this->load->view('myPages');
  }


}


Step 4: 

Add View Files application/views/myPages.php

<!DOCTYPE html>
<html>
<head>
	<title>My Pages for Alert</title>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet" />
</head>
<body>


<div>
	<?php
	  $this->load->view('alert');
	?>
</div>


</body>
</html>


application/views/alert.php

<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">


<script type="text/javascript">


<?php if($this->session->flashdata('success')){ ?>
    toastr.success("<?php echo $this->session->flashdata('success'); ?>");
<?php }else if($this->session->flashdata('error')){  ?>
    toastr.error("<?php echo $this->session->flashdata('error'); ?>");
<?php }else if($this->session->flashdata('warning')){  ?>
    toastr.warning("<?php echo $this->session->flashdata('warning'); ?>");
<?php }else if($this->session->flashdata('info')){  ?>
    toastr.info("<?php echo $this->session->flashdata('info'); ?>");
<?php } ?>


</script>

Now you can run this example