ItSolutionStuff.com

How to Implement Flash Messages in Codeigniter?

By Hardik Savani • November 5, 2023
Codeigniter

Today, i going to share with you how to display flash alert message in codeigniter application.

As we know, flash message(notification) feature is very important because client 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 your index page. Same as for others like any info, any errors come, any warning etc. So in this example i will learn how to display success message, error message, warning message and info message in codeigniter project.

we will use toastr plugin for display alert with pretty good layout. toastr provide us success, error, warning and info layout of notification. toastr plugin is open source and pretty easy to use. So let's proceed to 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 example is from scratch 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-flash'] = 'MyFlashController/success';

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

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

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

Step 3: Add Controller

In this step we require to create new MyFlashController 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/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

Now at last step we require to create "myPages.php" and "alert.php" view files. alert.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/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>

Ok, now we are ready to run our flash notification example. So let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:

http://localhost:8000/success-flash

http://localhost:8000/error-flash

http://localhost:8000/warning-flash

http://localhost:8000/info-flash

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Codeigniter Ajax Pagination using JQuery Example

Read Now →

Codeigniter Resize Image and Create Thumbnail Example

Read Now →

Codeigniter Drag and Drop Multiple Image Upload Example

Read Now →

Codeigniter Dynamic Dependent Dropdown using Ajax Example

Read Now →

Codeigniter JQuery Ajax Image Upload Example

Read Now →

Codeigniter Ajax Infinite Scroll Pagination Example

Read Now →

Codeigniter Image Upload with Validation Example

Read Now →

How to Get Last Inserted ID in Codeigniter?

Read Now →

Codeigniter Generate PDF from View using Dompdf Example

Read Now →

Codeigniter Ajax CRUD Tutorial Example

Read Now →

Laravel Implement Flash Messages Example

Read Now →

Codeigniter Select2 Ajax Autocomplete from Database Example

Read Now →

Laravel Toastr JS Notification Message Popup Example

Read Now →

JQuery Notification Popup Box using Toastr JS Example

Read Now →