CodeIgniter Flash Data
Solution 1
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>
Solution 2
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
Solution 3
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;?>
Solution 4
You can try this -
Controller:
$this->session->set_flashdata('success', 'Success Message...');
OR
$this->session->set_flashdata('error', 'Error Message...');
OR
$this->session->set_flashdata('warning', 'Warning Message...');
OR
$this->session->set_flashdata('info', 'Info Message...');
View:
<?php if($this->session->flashdata('success')){ ?>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Success!</strong> <?php echo $this->session->flashdata('success'); ?>
</div>
<?php } else if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Error!</strong> <?php echo $this->session->flashdata('error'); ?>
</div>
<?php } else if($this->session->flashdata('warning')){ ?>
<div class="alert alert-warning">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Warning!</strong> <?php echo $this->session->flashdata('warning'); ?>
</div>
<?php } else if($this->session->flashdata('info')){ ?>
<div class="alert alert-info">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Info!</strong> <?php echo $this->session->flashdata('info'); ?>
</div>
<?php } ?>
Tom
Updated on July 05, 2022Comments
-
Tom almost 2 years
I'm struggling with Flash Data in CodeIgniter.
I basically want to:
add a category to a database redirect user back to a page show a success pop-up message "Your category has been created"
So far I can add the category successfully to the db and the user input is validated correctly, only thing is I don't know how to create the pop up success message. (I don't want to load a success view), just redirect back to where they came from and show small message in the top corner or something.
Is flash data the right way to go?
-
Tom over 11 yearsCheers Dude! Now I can neaten this up with css and more javascript effects, Thanks!
-
Fifi over 5 yearsCould you explain why you should always redirect after a POST request ? Is it to unset session data ?
-
hndr over 5 yearsIts a common design pattern to prevent duplicate form submission: en.wikipedia.org/wiki/Post/Redirect/Get
-
P070 over 5 yearsJust noticed something that may want an adjustment. The closing brace for the php call needs to be in the document ready function. Otherwise it may give errors. $(document).ready(function() { $('.confirm-div').hide(); <?php if($this->session->flashdata('msg')){ ?> $('.confirm-div').html('<?php echo $this->session->flashdata('msg'); ?>').show(); <?php } ?> });
-
Mudshark over 5 years@Le-Nerdtm you are correct, I have adjusted my answer above.