Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data along with a redirect in CodeIgniter

I have a simple C (of CRUD) function, and I'd like to send a message (error or success) along with my redirect from the "insert" function I have written up. Is there a way to adhere a POST field with a redirect?

In pseudo code I have:

function view_all{     //set up some initial variables     $this->load->view(viewing_page, $data) }  function insert{     if ($this->db->insert(my_table, $_POST)){         $message = "All's well";     }     else {         $message = "whoops!";     }     redirect(view_all); } 

So the viewing_page ideally would have something like

if (isset($message)){     echo $message } 

So on the first time through, I don't see any message, and when/if there's an insert, it pops up the same page with the message. Thanks!

like image 280
Alex Mcp Avatar asked Dec 03 '09 03:12

Alex Mcp


People also ask

Can I pass data to redirect in CodeIgniter?

The CodeIgniter URL Helper comes to the rescue with the redirect() function that performs a header redirect to the path you specify as a parameter. redirect('/login/form/', 'refresh'); Just make sure you load the URL Helper prior to expecting this to work.

Can we pass data in redirect?

As part of the URL Redirect, you can pass along data that pertains to that survey response. This includes data collected in the survey response or data stored in hidden values or email campaigns, such as User ID for panel companies.


2 Answers

I believe redirect uses header(). If so, I don't believe you can send data along with a location header. You could accomplish the same thing using session vars or (not as good) appending a query string to the location URL.

For an 'accepted' way to do this in CodeIgniter look a little more than halfway down the session class documentation page.

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").

This (now deleted - here's an archived version) post on flash messages covers both the query string and the session var method.

Update: To summarize the now deleted post, it showed both urlencoding a message and appending as a query string (example from post):

header('Location: http://www.example.com/index.php?message='.urlencode($message)); 

And setting a 'flash' variable using two frameworks (example from post):

//Zend Framework $flashMessenger = $this->_helper->FlashMessenger; $flashMessenger->setNamespace('actionErrors'); $flashMessenger->addMessage($message);  //CakePHP $this->Session->setFlash('Your post has been saved.'); $this->redirect('/news/index'); 

Of course you can do roughly the same thing using $_SESSION directly (my example):

//first request $_SESSION['flash'] = 'This is a simple flash message.'; //next request $flash = $_SESSION['flash']; unset($_SESSION['flash']); //flash is one time only 
like image 134
Tim Lytle Avatar answered Sep 19 '22 08:09

Tim Lytle


You can use Flashdata in the CI Session Class. This is what's said in the document:

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").

Note: Flash variables are prefaced with "flash_" so avoid this prefix in your own session names.

To add flashdata:

$this->session->set_flashdata('item', 'value'); 

You can also pass an array to set_flashdata(), in the same manner as set_userdata().

To read a flashdata variable:

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

If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() function.

$this->session->keep_flashdata('item'); 
like image 45
Shaolin Avatar answered Sep 21 '22 08:09

Shaolin