问题
I am using the following codes to redirect my user to previous page after a particular task is done.
if (isset($_SERVER['HTTP_REFERER']))
{
$this->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']);
}
else
{
$this->session->set_userdata('previous_page', base_url());
}
The above code I use in a controller and the following code in another controller..
.... some other stuffs... I am updating database values here....
$this->db->where('t_expenseid', $t_expenseid);
query=$this->db->update('teacherexpense', $data);
redirect($this->session->userdata('previous_page'));
The above code is working fine but the problem I am facing is I want to send a success message with the redirect so that when the previous page loads a success message pops up (I already have jquery for that). And for this I added the following code above the redirect, but I don't know how to send the $data or the message along with the redirect. And if I am able send it how to retrieve the value in the controller of the previous page.
$data['msg']='Information Has been Successfully Inserted';
Could you please tell me how to send it and then retrieve it?
Thanks :)
回答1:
You can use set_flashdata of CI.You can use only once that message after refresh page message goes blank.
$this->session->set_flashdata('message', 'Authentication failed');
redirect(site_url('message/index/'), 'refresh');
And on that page you can catch this message by
$message = $this->session->flashdata('message').
回答2:
Consider using flashdata, which is generally used in situations where you want to redirect to a page and display a message. Keep in mind that the message will only be displayed once. If the user refreshes the page the message will disappear.
Here is how you would use it:
$this->db->where('t_expenseid', $t_expenseid);
query = $this->db->update('teacherexpense', $data);
// set flashdata
$this->session->set_flashdata('message', 'Information Has been Successfully Inserted');
redirect($this->session->userdata('previous_page'));
Then in the "previous page" you could check for a message and display it if it exists:
// get flashdata
$message = $this->session->flashdata('message');
if ($message) {
// pass message to view, etc...
}
来源:https://stackoverflow.com/questions/8576425/how-to-send-variable-data-when-redirecting-to-previous-page