How to set and get Session Flash message in codeigniter?
How to create flash data or message in Codeigniter
we need to store some data for only one time and after that we want to remove that data. For example, to display some error message or information message.
In CodeIgniter, flashdata will only be available until the next request, and it will get deleted automatically.
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
How to Add Flashdata?
$this->session->mark_as_flash('item');
mark_as_flash() function is used for this purpose, which takes only one argument of the value to be stored. We can also pass an array to store multiple values.
set_flashdata() function can also be used, which takes two arguments, name and value, as shown below. We can also pass an array.
$this->session->set_flashdata('item','value');
How to Get Flashdata?
Flashdata can be retrieved using the flashdata() function which takes one argument of the item to be fetched as shown below. flashdata() function makes sure that you are getting only flash data and not any other data.
$this->session->flashdata('item');
If you do not pass any argument, then you can get an array with the same function.
If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() function.
$this->session->keep_flashdata('item');
Example :- FlashData_Controller.php
<?php
class FlashData_Controller extends CI_Controller {
public function index() {
//Load session library
$this->load->library('session');
//redirect to home page
$this->load->view('flashdata_home');
}
public function add() {
//Load session library
$this->load->library('session');
$this->load->helper('url');
//add flash data
$this->session->set_flashdata('item','item-value');
//redirect to home page
redirect('flashdata');
}
}
?>
View :- flashdata_home.php
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter Flashdata Example</title>
</head>
<body>
Flash Data Example
<h2><?php echo $this->session->flashdata('item'); ?></h2>
<a href = 'flashdata/add'>Click Here</a> to add flash data.
</body>
</html>
Example :-
<?php
class Test extends CI_Controller {
public function index() {
$this->load->view('test_view');
}
// add record to db
public function add() {
// do your logics here
$this->session->set_flashdata('item', array('message' => 'Record created successfully','class' => 'success'));
$this->redirect('/test/index');
}
// edit record
public function edit() {
// do your logic here
$this->session->set_flashdata('item', array('message' => 'Record updated successfully','class' => 'success'));
$this->redirect('/test/index');
}
// delete record
public function delete($id) {
// do your logic here
$this->session->set_flashdata('item', array('message' => 'Record updated successfully','class' => 'success'));
$this->redirect('/test/index');
}
}
you can check and set failure messages also something like
if(true) {
$this->session->set_flashdata('item', array('message' => 'Record updated successfully','class' => 'success'));
} else {
$this->session->set_flashdata('item', array('message' => 'please try again!','class' => 'error'));
}
View file test_view.php :-
<?php
if(isset($this->session->flashdata('item'))) {
$message = $this->session->flashdata('item');
?>
<div class="<?php echo $message['class']"><?php echo $message['message']; ?></div>
<?php
}
?>
your html here
How to create flash data or message in Codeigniter
CodeIgniter session flash message. It’s a temporarily stored value. This coding used to set success message and failure message through your controller.
File Name :
$this->session->set_flashdata('MSG', "Record Added Successfully!!");
show flash data
// 'value1' will be erased after 250 seconds, while 'value2'
// will do so after only 300 seconds
$this->session->mark_as_temp(array(
'value1'=>250,
'value2'=>300
));
<?php if ($this->session->flashdata('MSG')) { ?>
<div role="alert" class="alert alert-success">
<button data-dismiss="alert" class="close" type="button">
<span aria-hidden="true">x</span><span class="sr-only">Close</span></button>
<strong>Well done!</strong>
<?= $this->session->flashdata('MSG') ?>
</div>
<?php } ?>
<?php echo $this->session->flashdata('MSG') ?>
Previous
Next