5

I have a jquery function, doing a $.post of a value to a php function. This php function then puts this value in the $_SESSION and shows the appropriate content.

I would like that the execution does not continue until the post is completed, since after the $.post the content is shown, and the new $_SESSION value does not take effect until the next refresh.

How can I do this?

Thanks!

luqita
  • 4,008
  • 13
  • 60
  • 93

2 Answers2

1

That's what callbacks are for. Pass a function to one of the $.post() parameters and it will be executed only when the request is successful:

$.post("test.php", function(data) {
   alert("Data Loaded: " + data);
});

document.write('This won`t wait for ajax');

this code will execute the alert() part only after the ajax request ends loading, but document.write() will execute straight away, without waiting for a response.

raveren
  • 17,799
  • 12
  • 70
  • 83
0

it sounds as though you may be making a more complicated peice of code than is necessary to accomplish what you want to accomplish.

if the client (javascript) has the data necessary to allow the server (PHP) to finish rendering the page during the page's load, then the client (javascript) must have gotten that data from the server (PHP) I would imagine. so if the server (PHP) had the data needed before it started rendering the page, it should have set the session variable itself before rendering.

this scenaraio doesn't seem to make a lot of sense. what is this data that the page is sending to the server using ajax during its loading? are you getting user input in any way during the page's load which you need to send back to the server to finish the page rendering? can we see any code?

dqhendricks
  • 19,030
  • 11
  • 50
  • 83