Welcome Guest, Not a member yet? Register   Sign In
flashdata and inserted rows
#1

[eluser]mdcode[/eluser]
I've tried various things over the last few days to get something in place, where the site displays an ok message if a row has been inserted into the database, and a failed message otherwise, but I'm just not getting it and need someone to point me in the right direction please. This is the code so far but am eminently willing to change it if there's an easier or better way:

CONTROLLER:
Code:
/* check if the form has been submitted */
if($this->input->post('submit_form'))
{
    $update = $this->projects_model->insert();
            
    if ($update > 0)
    {
    $flash['status'] = 'green';
    $flash['message'] = 'The project has been added successfully';
    }
    else
    {
    $flash['status'] = 'red';
    $flash['message'] = 'The project addition has failed.  Please try again.';
    }
            
}
        
$this->template->write_view('flash_message', 'default/pages/flash', $flash);
$this->template->write_view('content', 'default/admin/projects_add', $data);
$this->template->render();

This is part of my template:
Code:
<div class="content">
   &lt;?php echo $flash_message ?&gt;
   &lt;?php echo $content ?&gt;
</div>

And this is the error I am getting:
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: flash
Filename: controllers/projects.php
Line Number: 128
Code:
An Error Was Encountered
Cannot write to the 'flash_message' region. The region is undefined.
#2

[eluser]TheFuzzy0ne[/eluser]
OK, first of all, you are not using flash data correctly. In fact, you're not using flashdata at all. To use flashdata, you need to load the session class, and you set flashdata like so:

Code:
$this->session->set_flashdata('key', 'data');

Also, flash data is used to pass data over from one request, to the one after. If you are going to set it and use it in the same request, then you don't need flashdata, you just need a simple variable.

You could really do with initializing your variables before you use them, as currently, the $flash variable is only set if the form is being submitted. The only other alternative I can think of is to use isset() from within your view. That will fix those errors.

All the best!
#3

[eluser]Thorpe Obazee[/eluser]
Fuzzy,
Code:
$this->session->set_flashdata('key', 'data');
#4

[eluser]TheFuzzy0ne[/eluser]
Whoops. Thanks! Smile
#5

[eluser]mdcode[/eluser]
Whoops from me as well... and my thanks... while waiting for any repliess to my questions I carry on trying to solve things myself and I have something working for successful insertion into the database; not so much on the unsuccessful insertion but I guess the default messaging will have to do for now.




Theme © iAndrew 2016 - Forum software by © MyBB