开发者

getting a value from a php return in javascript

I'm using jquery's $.get() to find out how many entries are in a table. I want the returned value to be an Int but it seems to be something else. Ho开发者_如何学JAVAw do I work with this? this is the code I have. I'm sure I'm just going about this wrong. My background is many java.

var num = checksize();

function checksize(){
 $.get("../php/checktablezie.php", function(data){
  return data;
 });
}


You cannot do it like that. $.get is an asynchronous request, so your checksize() function will return instantly. Your anonymous function will be executed when the Ajax call is done, but that will be after checksize() has been returned.

Instead, you'll have to put whatever should be done with num inside your anonymous function, like this:

function checksize(){
 $.get("../php/checktablezie.php", function(data){
  num = data;
  //The code that is in need of num should be put in here.
  //e.g., if you're updating the GUI with the value, put that code here.
 });
}


Can we write functions like that for ansycronous ajax calls , as it may complete the call in parallel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜