Welcome Guest, Not a member yet? Register   Sign In
Displaying Messages and Notices

I'm looking for a simple way to display friendly messages to the user when actions are taken or when an action can't be performed—something like, "The ___________ entry has been updated" or "I'm sorry, that entry is locked and cannot be updated". How do you guys like to do this?

The problem I'm having is that I often use redirects after forms are successfully submitted to essentially clear all POST data, and I can't think of a clean way to display a message on the new page.

I appreciate any suggestions. Thank you.

[eluser]Glen Swinfield[/eluser]
Use a session - I do something like this;

have 2 session vars $_SESSION['message'] and $_SESSION['error_message'] - or as many as you like.

At the top of the view template I have:


   echo "<div class=\"message\">{$_SESSION['message']}</div>";


and the same for 'error_message'.

The if the $_SESSION is set it will display it then unset it so it won't appear again.

Great! Thanks. I actually thought about doing this but wasn't sure if I should be utilizing sessions in this way.

Thanks for the great reply.

[eluser]Glen Swinfield[/eluser]
I don't think it's a problem as long as you are not using loads of large session variables - and you're un-setting them after - all my CMS admin pages work like this and seem to work pretty well. ;-)

[eluser]Rick Jolly[/eluser]
Good solution Codepat. Just to round this out, there are replacement session libraries in the wiki that implement "flash" session data.

Flash data is kept in the session for only one request. That way you don't have to unset the message manually as in Codepat's example.

Just a variation of Codepat's example if you were to create a helper to get/unset session data:
&lt;?php if (isset($_SESSION['message'])): ?&gt;
<div class="message">&lt;?= sessionExtract('message'); ?&gt;</div>
&lt;?php endif; ?&gt;

// or
&lt;?php if (sessionIsSet('message')): ?&gt;
<div class="message">&lt;?= sessionExtract('message'); ?&gt;</div>
&lt;?php endif; ?&gt;

Have a look at the OBSession library. It's perfect for what you want.


Just a comment, but you could define your messages in a messages_lang file, load the lang file in a base controller's constructor, then use those messages in all controllers inheirted from (extended from) the base controller. This also assures that your application could support multiple languages if the need arises.

[eluser]Phil Sturgeon[/eluser]
If you dont fancy sessions you could try something like I have for my error message system.

function errorMessage($errorKey = 'error_unknown', $line = 0, $file = '')
    $extraDetails = '';
    if($line > 0) $extraDetails .= '/'.$line;
    if($file != '') $extraDetails .= '/'.basename($file);



[code]function error($errorKey = 'error_unknown', $line = 0, $file = 'unknown')
        $data = array(
            'errorMsg' => $this->lang->line($errorKey),
            'line' => $this->input->xss_clean(urldecode($line)),    
            'file' => $this->input->xss_clean(urldecode($file))    
        $this->template->create('error.html', $data);

Then to use it just write a new language entry for each message! You can ignore the line and file refferences, I just use __LINE__ and __FILE__ when calling the function to let me know where the problem is roughly.

Hope this code helps someone.

Can you use CI's in-built error message service to deliver good/success messages? Has anyone done this in the past? IE: Examples?

Theme © iAndrew 2016 - Forum software by © MyBB