I have a problem like this.I am using Code igniter to build a website.I want to pass a flash message when redirecting from a controller.For that I have make this code.
$data['message']=$this->session->set_flashdata('item', array('message' => 'Record created successfully','class' => 'success'));
var_dump($data['message']);
return;
redirect('question/index',$data);
Then i used var_dump to make sure that everything is work fine.but it print null.What is the issue of this.How can I fix this?
Full code for flash message :
Controller :
$query = $this->db->insert(table_name,$array_data);
if($query){
$this->session->set_flashdata('success', 'Sucessful added Multiple Image');
redirect($this->redirect);
}
else{
$this->session->set_flashdata('error', 'Something is wrong. Error!!');
redirect($this->redirect);
}
alert.php(view file)
<?php if ($this->session->flashdata('success')) { ?>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong><?php echo $this->session->flashdata('success'); ?></strong>
</div>
<?php } ?>
<?php if ($this->session->flashdata('error')) { ?>
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong><?php echo $this->session->flashdata('error'); ?></strong>
</div>
<?php } ?>
List.php(view file)
<?php
$this->load->view('alert');
?>
set_flashdata returns void, so it is reasonable that var_dump
shows nothing.
According to its manual:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
Which obviously means that you just need to do a new server request. A redirect, refresh, link or just something that redirects user to the next request.
Setting Flashdata in your Controller Method
$this->session->set_flashdata('success', "Password Changed");
Now Print Message in your view like this.
$data['message'] = $this->session->flashdata('success');