• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flashdata not displaying until refreshed

#1
[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.

#2
[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.

#3
[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.

#4
[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.

#5
[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

#6
[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?

#7
[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).

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

#9
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.