如何使用codeigniter flash变量?

时间:2013-09-09 06:52:50

标签: php jquery flash codeigniter

在CodeIgniter中与Flash数据斗争。

我基本上想:

向数据库添加问题重定向用户返回页面显示成功弹出消息“您的问题已创建”

到目前为止,我可以成功地将类别添加到数据库并正确验证用户输入,唯一的事情是我不知道如何创建弹出成功消息。 (我不想加载成功视图),只需重定向回到他们来自的地方,并在顶角或其他地方显示小消息。

闪存数据是正确的方法吗?

控制器: -

 $create_data =  $this->input->post();
    if(isset($create_data['question'])){
    $this->load->model('Test_model', 'test');
    $insert_status = $this->test->insertQuestions($create_data['question']);
    if($insert_status){
            echo "Record Inserted";
        }
        else{
            echo "Insertion Failed";
        }
    }
$this->layout->view('test/create');

2 个答案:

答案 0 :(得分:0)

function insert(){
    $create_data =  $this->input->post();
    if(isset($create_data['question'])){
        $this->load->model('Test_model', 'test');
        $insert_status = $this->test->insertQuestions($create_data['question']);
        if($insert_status){
            //echo "Record Inserted";
            $this->session->set_flashdata('msg', 'Record Inserted'); //set session flash
            redirect('controller_name/insert', 'refresh');
        }
        else{
            //echo "Insertion Failed";
            $this->session->set_flashdata('msg', 'Insertion Failed'); //set session flash
            redirect('controller_name/insert', 'refresh');
        }
    }else{
        $this->layout->view('test/create');    
    }
}
?>    

在查看页面中:

<p><?=$this->session->flashdata('msg')?></p>

答案 1 :(得分:0)

Flash数据听起来像是要走的路。你可以这样做:

if($insert_status){
    $notification = "Record Inserted";  
} else {
    $notification = "Insertion Failed";
}

$this->session->set_flashdata('notification', $notification);
redirect('controller/method','refresh');

然后使用

访问它

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

manual是此类信息的宝贵来源。