CodeIgniter: setting flash data not working

泪湿孤枕 提交于 2019-12-01 12:14:33

The first thing you should attend is once you call $this->session->flashdata('search-notes') method, it unsets the 'search-notes' item from session.

So, when you check the $this->session->flashdata('search-notes') at the second time, 'search-notes' will no longer exists.

If you want to keep the item in session, use set_userdata() and userdata() instead.

Also, you can use keep_flashdata('search-notes') after set_flashdata() or before the first call of flashdata() to preserve the flashdata variable through an additional request.

As a side point:
There's no need to check isset() and !empty() together. empty() does NOT generate a warning if the variable does not exist, and returns FALSE.

CI reference

There is also a nice tutorial on nettuts+ could be useful.


Jus as a Demo:
do NOT copy, check the logic.

if ($_POST['search-notes'] AND is_string($_POST['search-notes']))
{
    $post['search-notes'] = $this->input->post('search-notes'/*, TRUE*/ /* Enable XSS filtering */);
    $this->session->set_flashdata('search-notes', $post['search-notes']);
}
elseif ($searchNotes = $this->session->flashdata('search-notes'))
{
    $post['search-notes'] = $searchNotes;
}

if (! empty($post['search-notes']) AND is_string($post['search-notes'])):
// ...

If you need to keep the search-notes item in session, use the following at the first if statement:

if ($_POST['search-notes'] AND is_string($_POST['search-notes']))
{
    $post['search-notes'] = $this->input->post('search-notes'/*, TRUE*/ /* Enable XSS filtering */);
    $this->session->set_flashdata('search-notes', $post['search-notes']);
    // Keep the flashdata through an additional request
    $this->session->keep_flashdata('search-notes');

} // ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!