How to send variable data when redirecting to previous page

I use the following codes to redirect my user to the previous page after completing a specific task.

if (isset($_SERVER['HTTP_REFERER'])) { $this->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']); } else { $this->session->set_userdata('previous_page', base_url()); } 

The code above I use in the 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 works fine, but the problem I ran into is sending a message with a successful redirect message so that when the previous page loads, a success message will appear (I already have jquery). And for this, I added the following code above the redirect, but I don't know how to send $ data or a message along with the redirect. And if I can send it, how to get the value in the controller of the previous page.

  $data['msg']='Information Has been Successfully Inserted'; 

Could you tell me how to send it and then receive it?

Thanks:)

+6
source share
2 answers

You can use set_flashdata from CI. You can use only one message after the page update message.

  $this->session->set_flashdata('message', 'Authentication failed'); redirect(site_url('message/index/'), 'refresh'); 

And on this page you can catch this message

 $message = $this->session->flashdata('message'). 
+10
source

Consider using flashdata , which is commonly used in situations where you want to redirect to a page and display a message. Keep in mind that a message will only be displayed once. If the user refreshes the page, the message disappears.

Here's how you 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 on the "previous page" you can check the message and display it if it exists:

 // get flashdata $message = $this->session->flashdata('message'); if ($message) { // pass message to view, etc... } 
+2
source

Source: https://habr.com/ru/post/904156/


All Articles