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

Hi guys,

I'm having a small problem posting flashdata to my browser. I'm currently using this line of code for flashdata
$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.

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.

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

Basically, you'll need to load the URL helper:
and then redirect the user to the new 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.

[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

i have the url helper loaded.

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

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

how do i put my message through the data array then?

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.

function index()
    if ($this->input->post('submit'))
        $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:
<?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.