Welcome Guest, Not a member yet? Register   Sign In
How To Show Success Message In Codeigniter?
#1

(This post was last modified: 08-13-2020, 05:11 AM by php_rocs.)

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.

Downlaod Codeigniter 3
PHP Code:
<?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'


[b][b]application/controllers/firstcontroller.php[/b][/b]
PHP Code:
<?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');
  }




[b][b]application/views/MainPage.php[/b][/b]
PHP Code:
<!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> 


[b][b]application/views/alert.php[/b][/b]
Code:
<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>

Read More: Go To This Link phpcodingstuff.com/blog/how-to-show-success-message-in-codeigniter.html

Attached Files Thumbnail(s)
   
Reply
#2

Good start. But there is too much code for a simple task. You can do this with one method and without the selection statements.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB