CodeIgniter Forums
Flashdata not displaying until refreshed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Flashdata not displaying until refreshed (/showthread.php?tid=15849)



Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]macleodjb[/eluser]
Hi guys,

I'm having a small problem posting flashdata to my browser. I'm currently using this line of code for flashdata
Code:
$this->session->set_flashdata('message','You have successfully changed your security question');
                $this->load->view('account/change_security_question', $data);

Everything in the background executes as it should but my message does not appear until you try and refresh the page again. Anyone know why this is happening?

Thanks in advance.


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]pistolPete[/eluser]
From the user guide:

Quote:CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.



Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]macleodjb[/eluser]
Thanks but how do i fix it? Even if i redirect it still won't show the flashdata until i refresh the page.


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]TheFuzzy0ne[/eluser]
Basically, you'll need to load the URL helper:
Code:
$this->load->helper('url');
and then redirect the user to the new page:
Code:
redirect('/some_other_page');

If you are going to load the view inline, the you should be passing that message through in the $data array, and checking for it in the view.


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]pistolPete[/eluser]
[quote author="macleodjb" date="1234837541"]Thanks but how do i fix it?[/quote]

Have a look at these threads:

Session Flash Data, pointless?

FlashNotice Helper


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]macleodjb[/eluser]
i have the url helper loaded.

if i load it via the $data array doesn't defeat the whole purpose of flashdata?


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]TheFuzzy0ne[/eluser]
On the contrary. You use flashdata when you need a to pass data from one page to another. It's then deleted.

You are loading your page on the same request, but the flash data is only usable on the next request (which is why you see it when you refresh the page).


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]macleodjb[/eluser]
how do i put my message through the data array then?


Flashdata not displaying until refreshed - El Forum - 02-16-2009

[eluser]TheFuzzy0ne[/eluser]
Obviously you need to check the form has been submitted, and then run any validation on the incoming data. Within your logic, you'd set the $data variable with a message if necessary.

Controller:
Code:
function index()
{
    if ($this->input->post('submit'))
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('field1', 'Field 1', 'required');
        $this->form_validation->set_rules('field1', 'Field 2', 'required');
        if ($this->form_validation->run() === TRUE)
        {
            // Do stuff
            $data['message'] = 'You have successfully changed your security question';
        }
    }
    $this->load->view('account/change_security_question', $data);
}
The code above is untested.

Your view would then check to see if it's set, and if it is, it would display the message.

View file:
Code:
<?php if (isset($message)): ?>
    <p>&lt;?php echo $message; ?&gt;</p>
&lt;?php endif; ?&gt;

However, it's considered good practice to redirect your users to another page after a form has been validated successfully, as form data can be resubmitted if the user hits refresh. If this happens during a financial transaction, you'd end up purchasing the item more than once.That's where flash data comes in handy. Use flash data, and redirect the user back to the most sensible page, and read the flash data back to them.