Welcome Guest, Not a member yet? Register   Sign In
Simple question about displaying errors/messages
#1

[eluser]victorche[/eluser]
I know that it was discussed a lot, but anyway I can not find the best explanation for my case. CI has some kind of self included info/warning/error messages, but I need something different and easier. Here is my case:
Let's say I have a small comments form. Only input for name and textarea for comments.
I need... simply 2 types of messages. The first one for success, like "Thanks, your comment was added" and the second one will be an error like "Error, you should fill both of the fields".
I know how to check and validate the form. But I don't need a special success page like /comments/success

I just want after the form is submitted, the same page to be refreshed. Like redirect to the same page, e.g. /comments/ but this time with the message/error above. So the steps will be:
1. fill the form
2. submit
3. page refresh with error/message div
4. (not CI issue) the error/message box will become hidden after X seconds, thanks to jQuery :]

So how can I make this WITHOUT a special page like /comment/success ?
#2

[eluser]KingSkippus[/eluser]
There are a few ways to accomplish this. Probably the easiest is to, in your controller, check for $this->input->post('form_variable'), and if it finds it, sanity check the data and do whatever you want the form to do with it, and include a view at the top of the page that displays your "Thanks"/"Error" message as appropriate. If it doesn't find it, then it's not a post back and you should probably display the form.
#3

[eluser]Sean Gates[/eluser]
Use a the flash data within the session ... $this->session->set_flashdata(); and $this->session->flashdata(); ... always easiest in my book.
#4

[eluser]Zeeshan Rasool[/eluser]
@KingSkippus, nice explained. You can pass some param to differentiate about success or failur in a single function.
#5

[eluser]gigas10[/eluser]
On every page I have this code.
Code:
<?php if($this->session->flashdata('msg')){
    echo $this->session->flashdata('msg');
} ?>

Then whenever I want to set a message

Code:
$this->session->set_flashdata('msg', '<div id="success-msg">Issue Added</div>');

or

$this->session->set_flashdata('msg', '<div id="error-msg">ERROR: Issue already in system</div>');
#6

[eluser]Higher Ground Studio[/eluser]
I ajax all forms like that but the way that I set this up is when I return success or error messages I send the message in json.

I first send a signifier, usually using numbers (1 for success, 2 for error, 3 for warning...etc.), then the message.

Depending on the scalability need with the app I send the message formatted or unformatted and use js to format (I usually send html formatted though). Then when jquery gets the json returned it then knows what type of message that it received. I guess with this you could have a system that then does inline error messages or tool tips.

If you have any further questions about this just ask me.




Theme © iAndrew 2016 - Forum software by © MyBB