6

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 :)

black_belt
  • 6,601
  • 36
  • 121
  • 185

2 Answers2

10

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').
Vijaysinh Parmar
  • 897
  • 1
  • 15
  • 19
  • we did get the message through flash, but we lost the auto generated message by "validation_errors()", is there any way to catch them instead of writing our own message ? – Habib Rehman May 13 '16 at 07:02
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...
}
birderic
  • 3,745
  • 1
  • 23
  • 36