codeigniter flashdata not working as expected
Solution 1
Finally it works after a long effort. All you have to do is use $this->session->keep_flashdata('message')
with $this->session->unset_userdata('message')
here is my solution (view file)
<?php
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
$this->session->unset_userdata('message');
}
?>
After that in my controller construct
function
public function __construct() {
parent::__construct();
.....
$this->session->keep_flashdata('message');
}
it works in each error. still have some silly issue but working nicely so far
Solution 2
Try this..
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
$this->session->unset_userdata('message');
}
Rejoanul Alam
Updated on June 13, 2022Comments
-
Rejoanul Alam almost 2 years
I have set flashdata in controller like
public function customer() { $data['title'] = 'Black List Management'; if ($this->input->post('single_black')) { //echo 'here';return; $wallet = trim($this->input->post('single_wallet', TRUE)); $reason = trim($this->input->post('reason', TRUE)); $match = preg_match('/^01[15-9]\d{8}$/', $wallet); //if not valid mobile if ($match == 0 || !$match) { $this->session->set_flashdata('message', 'The wallet is not valid Mobile no.'); redirect('blacklist/index'); } $is_blacklisted = $this->db->where('wallet', $wallet)->where('is_blacklisted', 1)->get('customers')->num_rows(); //if already blacklisted if ($is_blacklisted > 0) { $this->session->set_flashdata('message', 'This wallet is already in blacklist'); redirect('blacklist/index'); } $this->form_validation->set_rules('reason', 'Reason', 'required'); if ($this->form_validation->run() == FALSE) {// if invalid form $this->nuts_lib->view_loader('user', 'blacklist', $data); return; } else { $user_id = (int) $this->session->user_id; $query = $this->db->where('wallet', $wallet)->where('is_blacklisted', 0)->get('customers'); $result = $query->result_array(); if (count($result) > 0) {// if exist uppdate $customer_id = (int) $result[0]['id']; $blacklist = array( 'is_blacklisted' => 1, 'blacklist_meta' => date('Y-m-d H:i:s') . '|' . $user_id . '|' . $reason ); $this->db->where('id', $customer_id)->update('customers', $blacklist); } else {// insert $new_blacklist = array( 'wallet' => $wallet, 'is_blacklisted' => 1, 'blacklist_meta' => date('Y-m-d H:i:s') . '|' . $user_id . '|' . $reason ); $this->db->insert('customers', $new_blacklist); } $this->session->set_flashdata('message', 'Successfully Blacklisted'); redirect('blacklist'); } } }
From this
customer
method redirecting to following index method when errorpublic function index() { $data['title'] = 'Black List Management'; $this->nuts_lib->view_loader('user', 'blacklist', $data); }
In my view file (user/blacklist.php)
$message = $this->session->flashdata('message'); if (isset($message)) { echo '<div class="alert alert-info">' . $message . '</div>'; }
So when get
$error
its showing flashdata nicely, But problem is when get same error next time (after submitting form) then flashdata not show anymore.What I have tried so far is CodeIgniter flashdata not working after redirect
I need to show
flashdata
message evry time when get$error