CodeIgniter: setting flash data not working

前端 未结 1 1918
有刺的猬
有刺的猬 2021-01-16 04:31

I\'m using the following code to manage pagination in search results:

if ($this->input->post(\'search-notes\') && (is_string($this->input-&g         


        
相关标签:
1条回答
  • 2021-01-16 05:06

    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');
    
    } // ...
    
    0 讨论(0)
提交回复
热议问题