views:

40

answers:

1

In my backend I'm using jquery 1.4.1 and the newest UI 1.8rc1. I defined a couple of buttons that do things... one is create a certain type of page using serialize functions calling a php file and then reloading the entire page. locally this always works like a charm! but as soon as i put it on my providers webserver, it only works in about 5% of times. Heres the code:

    buttons: {
'Seite erstellen': function() {
$.post("webadmin/pages.create.serialize.php",$("#page-form").serialize());
$(this).dialog('close');
location.reload(true);
},
'Abbrechen': function() {
$(this).dialog('close');
}
},

Where it gets interesting is, when I put in an alert just before the location.reload part - it will always work. So there seems to be a timing issue that the serializing is executed but can't finish before the page reloads. i know the meaning of using the serialzing is not to have to reload the page, but i build a navigation etc. so i need to reload. (thinking about that now... i could really serialize everything... anyway) Is there a simple solution to this? is there something like a little timer i could build in to make it wait until the serialization is done? is this a normal behaviour?

+1  A: 

You need to take advantage of the callback in the $.post() method:

$.post(
       "webadmin/pages.create.serialize.php",
       $("#page-form").serialize(),
       function(data, textStatus, xhr) {  
           alert("I'm done loading now!");
       }
);

Not exactly sure what "this" refers to inside of the callback function so I'll leave the implementation as an exercise to the reader. :-)

roufamatic
"this" refers to the dialog object that the buttons object is a part of. So to be correct, he'd need to define something like "var self = this;" inside in the dialog but outside the callback, and then use "self" instead of "this" inside the callback.
Mike Sherov
Thanks, I was going to suggest that but then questioned whether there was a better way to avoid the var x = this bit.
roufamatic
Thanks - that all works like a charm now!And I decided to quit the reloading all together and update all the divs in the callback!
Mark Nolan