views:

1023

answers:

1

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!

+6  A: 

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 getvar 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 post on flash messages covers both the getvar and the session var method.

Tim Lytle
Aah, good info, thanks. Their docs are really great, but just so long...
Alex Mcp
Session is the only way to accomplish this unless you use query strings which get messy with the URI routing that CI has. +1 for this answer.
Jessedc