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

[eluser]BrandonDurham[/eluser]
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.
#2

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

Code:
<?php

if(isset($_SESSION['message'])){
   echo "<div class=\"message\">{$_SESSION['message']}</div>";
   unset($_SESSION['message'];
}

?&gt;

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.
#3

[eluser]BrandonDurham[/eluser]
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.
#4

[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. ;-)
#5

[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.
http://codeigniter.com/wiki/Category:Lib...::Session/

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.

EDIT:
Just a variation of Codepat's example if you were to create a helper to get/unset session data:
Code:
&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;
#6

[eluser]Crimp[/eluser]
Have a look at the OBSession library. It's perfect for what you want.

http://bleakview.orgfree.com/obsession/
#7

[eluser]esra[/eluser]
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.
#8

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

Helper
Code:
&lt;?php
function errorMessage($errorKey = 'error_unknown', $line = 0, $file = '')
{
    $extraDetails = '';
    
    if($line > 0) $extraDetails .= '/'.$line;
    if($file != '') $extraDetails .= '/'.basename($file);

    redirect('/admincp/dashboard/error/'.$errorKey.$extraDetails);

}


[b]Controller[/b]
[code]function error($errorKey = 'error_unknown', $line = 0, $file = 'unknown')
    {        
        $this->lang->load('error');
        
        $data = array(
            'errorMsg' => $this->lang->line($errorKey),
            'line' => $this->input->xss_clean(urldecode($line)),    
            'file' => $this->input->xss_clean(urldecode($file))    
        );
        
        $this->template->title('Error');
        $this->template->create('error.html', $data);
    }
?&gt;[/code]

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.
#9

[eluser]worchyld[/eluser]
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