Welcome Guest, Not a member yet? Register   Sign In
Ajax requests destroys flashdata
#1

[eluser]cimmerian[/eluser]
I've noticed that any ajax requests that are performed after flashdata has been set will clear the flashdata. Is there any way to combine the use of ajax and flashdata, or should it just be avoided?

Preferably the controllers that responds to the ajax requests should be able to make a function call to preserve all flashdata without having to know the names of the flashdata variables.
#2

[eluser]BrianDHall[/eluser]
As far as I am aware this is just one of the weaknesses of flash data.

One way to handle it, which is basically a hack but it would work just fine for your use if you are already rather heavily invested into using flashdata, is a custom function you set that would foreach through every flashdata variable and set it again as a flash variable - you would call this function whenever you wanted to preserve your variables.

The other, far easier, option is just use regular session variables and manually unset them when you are done with them (so if X message printed to browser, unset message X... etc). Or if you don't really care when the variable dies, let the garbage collector handle it for you Smile

I would definately suggest the latter as the 'best way', as I don't believe there is any "ignore this request as far as flashdata goes" option built into codeigniter, though it wouldn't necessarily be a bad option.
#3

[eluser]adamk[/eluser]
As a newbie with JQUERY I struggled with this for two days. I was implementing a site that used AJAX for search suggestions but was inadvertently deleting the flashdata I used to store the user's search queries if the user decided to sort the results. I tried everything including writing a keep_all_flashdata function that I called in the AJAX function hoping that would work. When that didn't work I searched around and I finally found the solution which is very simple.

Simply extend the Session library with MY_Session in the applicaiton/libraries folder to include

/*
** sess_update
**
** skip update on AJAX requests
**
** http://ellislab.com/forums/viewthread/138823/
**
*/

function sess_update()
{
// skip the session update if this is an AJAX call!
if ( ! IS_AJAX )
{
parent:Confusedess_update();
}
}

AND in the application/config/constants.php add

// Define Ajax Request
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');


Those two additions solved the erasing of flashdata with ajax. Hope that helps!!!
#4

[eluser]Mr. Fulop[/eluser]
I came across the same issue regarding the loosing of the flashdata after an ajax request.
@adamk Thanks for the solution!
#5

[eluser]Kindari[/eluser]
You can cycle through session->all_userdata(), and look for the flashdata keys, then on those keys call keep_flashdata(). This will probably be better as an extension for the session class.




Theme © iAndrew 2016 - Forum software by © MyBB