Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...