Kate Wintz
Kate Wintz

Reputation: 643

CodeIgniter "flashdata" doesn't work

I use CodeIgniter 2.1.0, i want after insert data in database get a message like "Your information was successfully updated.". For this work i have in CI_Controller following function:

function myCiInser(){
... Here is my query ...
//$data: this var is result query that is true
if($data){
    $this -> session -> set_flashdata('message', 'Your information was successfully updated.');
    redirect('url/myurl');
            }
}

And i have in view as:

<?php
$message = $this->session->flashdata('message');
    if($message){
        echo '<div id="error_text">' . $message . '</div>';
    }
//I test this : "echo $message;" but don't give output
?>

But i don't give message in view but redirect is done and work true. and in database in table ci_sessions column user_data i have this:

a:2:{s:9:"user_data";s:0:"";s:19:"flash:new:message";s:42:"Your information was successfully updated.";}

How can fix this problem?

UPDATE:

I had the following error (i use from chorme and by Ctrl+Shift+j i get this alert):

Failed to load resource: the server responded with a status of 404 (Not Found)

And i fix it (Now i do not have the error) but still is same problem in display message. what do i do?

Upvotes: 11

Views: 41047

Answers (12)

heySushil
heySushil

Reputation: 499

I use this for flash data and it's easy to use. first, need to you have creat session and then in your controllers' method use this just before where you want to redirect your page.

On Controller after creating a session and don't forget to load session and url library.

$this->session->set_flashdata('success', 'Oops. This email id already exist.' );
redirect("You Mehod or page");

In this case, you no need to go to your particular view page to add extra php code.

And on footer.php in view past this script code

<!-- Code for flashdata toaster -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
<script type="text/javascript">
    <?php if($this->session->flashdata('success')){ ?>
        toastr.success("<?php echo $this->session->flashdata('success'); ?>");
    <?php }else if($this->session->flashdata('error')){  ?>
        toastr.error("<?php echo $this->session->flashdata('error'); ?>");
    <?php }else if($this->session->flashdata('warning')){  ?>
        toastr.warning("<?php echo $this->session->flashdata('warning'); ?>");
    <?php }else if($this->session->flashdata('info')){  ?>
        toastr.info("<?php echo $this->session->flashdata('info'); ?>");
    <?php } ?>
</script>
<!-- End of flashdata script -->

Good luck and hope it will help for your problem.

Upvotes: 0

qwertzman
qwertzman

Reputation: 782

I had Chrome developer console open and flashdata was removed. After closing it and retrying it works. Version 71.0.3578.98 (Official Build) (64-bit)

Upvotes: 0

Petr
Petr

Reputation: 11

I had the same problem. After checking the code I have found, that I am calling $this->session->sess_destroy();, which causes the problem.

Upvotes: 1

Alok Babu
Alok Babu

Reputation: 101

Except one page, I am able to display/pass values using session. I tried using the var_dump($this->session) and I get:

["flash:old:Array"]=> bool(false)
["flash:new:message"]=> string(10) "My Message"

I have tried echoing the flash data within the page without redirecting just after setting the data, but the result was same. I recommend to trim down the code, and try to set session in other pages. If the problem persists check your var_dump. This might not be the solution, but I think it can help.

UPDATE : trimming down spaces and newlines within the text worked. I was passing 2 long sentences with empty line breaks and spaces.

if (0) //Assume this condition is false
{   
    $this->load->view('error_page');
    // Generate validation error
}
else
{
    //Show success message
    $data = array(
                'message' => 'My message'
                           );
    $this->session->set_flashdata($data);
    $this->session->keep_flashdata($data);                      
    echo $this->session->flashdata('message');
    //echo var_dump($this->session);
    //redirect(base_url().'success_page');
}

Upvotes: 1

Abdulqader Kapadia
Abdulqader Kapadia

Reputation: 21

Using sessions with database has caused me issues at times. I recommend setting $config['sess_use_database'] = FALSE; in the config.php and see if the flashdata works fine.

Upvotes: 2

Rishabh Malhotra
Rishabh Malhotra

Reputation: 1

I know I am very late but I was having this problem and I couldn't believe that in my case the solution was very easy

just replace

$this->session->flashdata('message');

to

print_r($this->session->flashdata('message'));

Upvotes: 0

dpkrai96
dpkrai96

Reputation: 93

As I am observing about codeigniter flashdata. When I use it in second request using codeigniter redirect() method it is working fine in mozila but in the case of chrome it is not working.

Upvotes: 0

Sujeet Kumar
Sujeet Kumar

Reputation: 1320

// Set flash data in our controller file

$this->session->set_flashdata('sessionkey', 'Value');

// After that we need to used redirect function

redirect("admin/signup");

// Get Flash data on view

$this->session->flashdata('sessionkey');

Upvotes: 3

Quetzy Garcia
Quetzy Garcia

Reputation: 1840

From the Codeigniter Session Class documentation, regarding Flashdata we can read:

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.

Your problem might be that when you redirect, the process takes more than one request, clearing your flashdata.

To see if that's the case, just add the following code to the constructor of the controller you are redirecting to:

$this->session->keep_flashdata('message');

This will keep the flashdata for another server request, allowing it to be used afterwards.

Upvotes: 18

apis17
apis17

Reputation: 2855

404 (not found) count as 1 server request. it will remove your flashdata.

Upvotes: 0

Vinicius
Vinicius

Reputation: 121

I had that problem too. I don't remember where I saw but here's my solution.

redirect('url/myurl','refresh');

CodeIgniter didn't treated redirect as another request. So flashdata wasn't set in the redirect, but it was on the next page I loaded.

Upvotes: 12

Tihomir Mihaylov
Tihomir Mihaylov

Reputation: 835

You can also use database for the sessions, but you have to set the config items:

$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;

In that way the session flashdata will work again

Upvotes: 2

Related Questions