Reputation: 2364
$.post("general.php", {/* some values */}, function(data){var id = data;});
alert(id);
The problem is that, alert is empty. Do know anyone why? And how to fix it? Thanks.
Upvotes: 0
Views: 67
Reputation: 41533
There are 2 problems :
one is that the ajax is asynchronous and the alert executes before the ajax callback, where the id
variable is being set
the second one is that the variable is not global, it is only visible in the callback scope
So, I suggest you declare the variable global (if you need it for later use) :
var id;// in the global scope
or
window.id = '';
And you should probably execute the logic in the callback :
$.post("general.php", {/* some values */}, function(data){
window.id = data;
alert(data);
});
Upvotes: 6