CodeIgniter Forums

Full Version: Show message rather than load a view
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]alan-tucker[/eluser]
How do I display a success message rather than loading a view/going to a new page?

Thanks

El Forum

[eluser]kurucu[/eluser]
That's not directly a feature of the framework, so you'd have to use the framework to develop something to do this yourself. However, initial thoughts are that you look at Flash Data (in the Session library), Ajax, using a view (I know you said without doing this, but I suspect this is the answer!)

Really you need to provide more information though. When does this message get displayed? Has the user click a link, or has an error been generated in the controller... or?

I have extended the Controller class and have a function "message", this takes several parameters: title, message, type, destination. It actually then loads a message view, inserts the relevant data, has an OK button linked to the destination and is styled and makes logs according to type. After calling it I generally call return. For me, although this means I don't have swish little confirmation boxes, it means I can quickly and simply generate errors (auth and code), notices and confirmations from any controller with predictable results. I intend later to provide another function standard_message which, based upon the standard identifier passed to it, pulls in a message from the DB and generates that. But that's another story.

El Forum

[eluser]alan-tucker[/eluser]
Thanks for the reply.

As an example if no users are found in the database they i would like to just show a message rather than having to load a view with the message "no users round." etc.

El Forum

[eluser]BrianDHall[/eluser]
Really simple answer: echo.

Generally you run your form validation method (or whatever test condition you have) in an 'if' statement, and if success you either load a 'success' view or you just echo directly what you want to say.

If you want to display a success message without refreshing the page, then you'll need javascript.

El Forum

[eluser]alan-tucker[/eluser]
thanks for the reply.

using an "echo" is not really the correct solution. I want to store what would be the message in a local variable which I can then output on the page in a specif posistion. also i dont want to reply on javascript.

El Forum

[eluser]kurucu[/eluser]
It sounds like flashdata might be the answer then. Or some similar construct in your preferred session handler.

Either way the concept is this:

1. For each message generated, you queue it into a session object somewhere (flashdata if you use CI's library)
2. You load the messages into the view variables (you can load global view data, I forget the function name)
3. You have a hook in your parent view (template) that looks for this data and displays it if set.

With CI, flashdata is automatically erased at the end of the next request, so the user would only see it once. You'd have to implement this yourself if you choose another method without it.

Edit: Re-reading your reply, it sounds like the message is to be posted during the same request, in which case you can just build a message queuing system (an array lol) which you look for and output in the view.

El Forum

[eluser]alan-tucker[/eluser]
Will give Flashdata a try

Thanks