How To Show Success Message In Codeigniter?


Today we will learn how to display flash alert messages in Codeigniter application.

As we know, flash message(notification, alert) feature is very important because clients can understand their work is success or any error like if you create new user successfully then after create user successfully flash message will come on main page. Same as for others like any type of error like success, error, warning etc. So in this example, I will learn how to display all type messages (alert),  in Codeigniter project.

we will use toastr for display alert with good layout. toastr provide us any type of alert layout of notification. toastr is open source and easy to use. So let's start the make example from scratch.

Step 1: Download Fresh Codeigniter 3

In First step 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 step, we will add one routes for demo. this tutorial so you can check each alert with demo. So let's add bellow 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'] = 'firstcontroller/success';
$route['error'] = 'firstcontroller/error';
$route['warning'] = 'firstcontroller/warning';
$route['info'] = 'firstcontroller/info';
Step 3: Add Controller

In this step we require to create new firstcontroller controller, In that controller file we will write method for each flash example. So let's add with following code. you have to just copy of welcome.php controller file:

application/controllers/firstcontroller.php

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

class firstcontroller extends CI_Controller {

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

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

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

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

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


}
Step 4: Add View Files

Now at last step we require to create "MainPage.php" and "alert.php" view files. alert.php file you can include all the files so each time you can easy generate notification messages in your project. So let's create both files:

application/views/MainPage.php

<!DOCTYPE html>
<html>
<head>
	<title>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>

Demo:

How To Show Success Message In Codeigniter? - Php Coding Stuff

Now you can open bellow URL on your browser:

http://localhost:8000/success

http://localhost:8000/error

http://localhost:8000/warning

http://localhost:8000/info


I hope it can help you...

  1. You’re a beautiful inspiration. It really helps me in any situation. Where I stuck. Many of my friends told me to comment there post but I stuck what I should comment. Finally I got your post it always help me. Thanks for the lovely post.

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close