Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and jquery: waiting for $.post

Tags:

jquery

php

.post

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!

like image 294
luqita Avatar asked Nov 13 '22 21:11

luqita


1 Answers

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.

like image 131
raveren Avatar answered Dec 22 '22 05:12

raveren