Reputation: 5398
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 error
public 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
Upvotes: 2
Views: 24611
Reputation: 1
got same issues in CI 3.1.11, have upgraded to CI 3.1.13 and it works fine.
Upvotes: 0
Reputation:
Add this construct function in your email sent controller:
public function __construct() {
parent::__construct();
$this->session->keep_flashdata('message');
}
Upvotes: 1
Reputation: 5398
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
Upvotes: 6
Reputation: 2617
This is how you set Flash message in CI
$this->session->set_flashdata('message','number is not valid');
redirect('blacklist/index'); //Redirect after setting flash message
After Redirect In your view page create DIV
<div class="confirm-div alert alert-info"></div>
Add this before </body >
<script>
// assumes you're using jQuery
$(document).ready(function() {
$('.confirm-div').hide();
<?php if($this->session->flashdata('message')){ ?>
$('.confirm-div').html('<?php echo $this->session->flashdata('message'); ?>').show();
<?php } ?>
});
</script>
Upvotes: 0
Reputation: 1823
I do this on most projects I build in CI. This is how I do it (The HTML markup is for Foundation);
The controller:
$this->session->set_flashdata('error', 'Invalid registration number');
redirect();
The view:
if ($error = $this->session->flashdata('error')) {
echo '<div data-alert class="alert-box alert">';
if (is_array($error))
{
foreach($error as $e)
{
echo '<p class="error">' . $e . '</p>';
}
}
else
{
echo $error;
}
echo '<a href="#" class="close">×</a>';
echo '</div>';
}
Hope this helps.
Upvotes: 0
Reputation: 22532
Instead of getting flash message in view you can get it into your controller and pass it into your view
Controller
<?php
if($error){
$this->session->set_flashdata('message','number is not valid');
redirect('blacklist/index');
}
function index()
{
$error = $this->session->flashdata('message');// get you flash message
$data = array();// create array
//...
$data['message'] = $error;// pass you message to array
$this->load->view('someview',$data);// pass your message to your view
}
?>
View
// And in your view file
<?php if($message):
echo '<div class="alert alert-info">' . $message . '</div>';
endif; ?>
Upvotes: 0
Reputation: 55
The problem you are facing is a quite a simple one. It will only display for the first time because it will not check for the second time if the error comes in. The reason behind it is you have placed the flash data in a IF ..ELSE statement..which will only work for only single time.
Try this one:
<?php if($this->session->flashdata('message')):?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('message');?>
</div>
<?php endif ?>
I think this will work for you ...
Upvotes: 0
Reputation: 473
Try this..
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
$this->session->unset_userdata('message');
}
Upvotes: 1