dtlc84438
dtlc84438
2018-01-02 02:22
浏览 28
已采纳

如何在codeigniter中正确设置flash_message?

I have a problem like this.I am using Code igniter to build a website.I want to pass a flash message when redirecting from a controller.For that I have make this code.

$data['message']=$this->session->set_flashdata('item', array('message' => 'Record created successfully','class' => 'success'));
var_dump($data['message']);
return;
redirect('question/index',$data);

Then i used var_dump to make sure that everything is work fine.but it print null.What is the issue of this.How can I fix this?

图片转代码服务由CSDN问答提供 功能建议

我有这样的问题。我正在使用Code igniter构建一个网站。我想传递一条flash消息 当从控制器重定向时。为此,我已经制作了这段代码。

  $ data ['message'] = $ this-> session-> set_flashdata('item',array('message'=>'记录创建成功'  ,'class'=>'成功')); 
var_dump($ data ['message']); 
return; 
redirect('question / index',$ data); 
   
 
 

然后我使用var_dump来确保一切正常。但是它打印为null。这是什么问题。我该如何解决这个问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • ds3016
    ds3016 2018-01-02 13:33
    已采纳

    Full code for flash message :

    Controller :

          $query = $this->db->insert(table_name,$array_data);
    
          if($query){
            $this->session->set_flashdata('success', 'Sucessful added Multiple Image');
            redirect($this->redirect);
          }
          else{
            $this->session->set_flashdata('error', 'Something is wrong. Error!!');
            redirect($this->redirect);
          }
    

    alert.php(view file)

    <?php if ($this->session->flashdata('success')) { ?>
    
            <div class="alert alert-success">
              <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                <strong><?php echo $this->session->flashdata('success'); ?></strong>
            </div>
    
    <?php } ?>
    
    <?php if ($this->session->flashdata('error')) { ?>
    
            <div class="alert alert-danger">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                <strong><?php echo $this->session->flashdata('error'); ?></strong>
            </div>
    
    <?php } ?>
    

    List.php(view file)

    <?php
        $this->load->view('alert');
     ?>
    
    点赞 评论
  • duan4523
    duan4523 2018-01-02 02:24

    set_flashdata returns void, so it is reasonable that var_dump shows nothing.

    According to its manual:

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

    Which obviously means that you just need to do a new server request. A redirect, refresh, link or just something that redirects user to the next request.

    点赞 评论
  • dongxiong1935
    dongxiong1935 2018-01-02 12:39

    Setting Flashdata in your Controller Method

    $this->session->set_flashdata('success', "Password Changed");

    Now Print Message in your view like this.

    $data['message'] = $this->session->flashdata('success');

    点赞 评论

相关推荐