Welcome Guest, Not a member yet? Register   Sign In
Send message to the view from controller on validation success
#1

[eluser]Unknown[/eluser]
I have a view with 3 forms with the action attribute set to /controller/submit for all of the forms. For the controller, I check ($this->input->post('submit_name')) to manage correctly the data for the specific submitted form.

If there are any errors, I reload the view calling $this->index();
If there aren't, I add into the database some records and I also call $this->index();

My question is:
It is possible to send a $message to the view if the form is submitted correctly?
(e.g. for every input->post('submit_name') set a custom message like 'That form has been submitted correctly');

I tried something like this:
In the view I added
Code:
if (@$message) echo $message;
Note the '@' sign. If $message is not set, it won't echo out any notifications about $message.

If the form is submitted:
Code:
$data['message'] = 'The category has been added successfully!';
$this->load->view('same/view', $data);
I don't know why, the message doesn't appear. (and some errors do appear).

Please let me know if you tried something like this and found a solution.
Thanks
#2

[eluser]vitoco[/eluser]
i think that the "@" is used to suppress errors, but what you really need is to use "isset" function
Code:
if( isset( $message ) )
{
    echo $message
}

Saludos
#3

[eluser]Unknown[/eluser]
Thanks for the quick reply, vitoco!
I knew that '@' is used to suppress the errors (if $message is not set). For the view, I think isset is more appropriate.

I think I found the solution to pass $message.
In my index() function of the controller I added this:
Code:
function index($message = '') { $data['message'] = $message; }

If one form is submitted successfully, I set $message to something and then:
Code:
$this->index($message);

In the view I add another condition to hide the message if nothing is sent:
Code:
if ( (isset($message)) && ($message !== '') ) { echo $message;

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB