CodeIgniter Flash Data CodeIgniter Flash Data codeigniter codeigniter

CodeIgniter Flash Data


In your controller:

//add to db// load session library if not auto-loaded$this->session->set_flashdata('msg', 'Category added');redirect('controller/method');

In the view:

<script>// assumes you're using jQuery$(document).ready(function() {$('.confirm-div').hide();<?php if($this->session->flashdata('msg')){ ?>$('.confirm-div').html('<?php echo $this->session->flashdata('msg'); ?>').show();<?php } ?>});</script>


Your can perform different session message depends what you pass to view from your controller. Noted that I am using Bootstrap as my CSS backbone.

In view,

For success case,

<?php if ($this->session->flashdata('category_success')) { ?>        <div class="alert alert-success"> <?= $this->session->flashdata('category_success') ?> </div>    <?php } ?>

For error case,

<?php if ($this->session->flashdata('category_error')) { ?>    <div class="alert alert-danger"> <?= $this->session->flashdata('category_error') ?> </div><?php } ?>

In controller,

For success case,

$this->session->set_flashdata('category_success', 'Success message.');redirect("To your view");

For error case,

$this->session->set_flashdata('category_error', 'Error message.');redirect("To your view");

For more reference you can visit: http://www.codeigniter.com/userguide2/libraries/sessions.html


using ternary operator :

Setting Flash Data:

$this->session->set_flashdata('insertproduct', 'Product added successfully');$this->session->set_flashdata('deleteproduct','Delete added successfully');

Using the Flash Session Data:

<?php if($this->session->flashdata('insertproduct')):echo $this->session->flashdata('insert');endif; ?><br/><?php if($this->session->flashdata('delete')): echo $this->session->flashdata('delete'); endif;?>