how to display success message in codeigniter?

Today, We want to share with you how to display success message in codeigniter.In this post we will show you codeigniter flash message timeout, hear for codeigniter flash message bootstrap we will give you demo and example for implement.In this post, we will learn about notification in php demo with an example.

How to implement flash messages in PHP Codeigniter?

Step 1: Download Fresh Codeigniter 3

In First phase we will download fresh version of Codeigniter 3, so if you haven’t download yet then download from here : Download Codeigniter 3.

Step 2: Add Route

In this phase, we will add one routes for demo. this simple example is from step by step so you can check each flashmessages with demo. therefor let’s add bellow website routes in route file.
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-product'] = 'ProductDetailsController/success';
$route['error-product'] = 'ProductDetailsController/error';
$route['warning-product'] = 'ProductDetailsController/warning';
$route['info-product'] = 'ProductDetailsController/info';

Step 3: Add Controller

In this phase I need to create new ProductDetailsController controller, In that controller file we will write method for each flash product example.therefor let’s add with following code. you have to just copy of main file like as a welcome.php controller file:
application/controllers/ProductDetailsController.php

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


class ProductDetailsController extends CI_Controller {

   public function __construct() { 
      parent::__construct(); 
      $this->load->library("session");
   }

	public function success()
	{
      $this->session->set_flashdata('success', 'Product Updated successfully');
      return $this->load->view('productList');
	}

  public function error()
  {
      $this->session->set_flashdata('error', 'Something is wrong.');
      return $this->load->view('productList');
  }


  public function warning()
  {
      $this->session->set_flashdata('warning', 'Something is wrong.');
      return $this->load->view('productList');
  }


  public function info()
  {
      $this->session->set_flashdata('info', 'Product listed bellow');
      return $this->load->view('productList');
  }


}

Step 4: Add View Files

Now at last phase we require to create “productList.php” and “flashmessages.php” view files. flashmessages.php file you can include on all the files so each time you can simply generate notification messages. So let’s create both files:
application/views/productList.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('flashmessages');
	?>
</div>


</body>
</html>

application/views/flashmessages.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>

Run URL

http://www.your-domain-name.com/success-product

http://www.your-domain-name.com/error-product

http://www.your-domain-name.com/warning-product

http://www.your-domain-name.com/info-product

I hope you get an idea about codeigniter alert message in controller.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.