pass parameter from $.post() callback to outer variable

Posted by Nazmin on Stack Overflow See other posts from Stack Overflow or by Nazmin
Published on 2010-04-19T15:44:06Z Indexed on 2010/04/20 7:43 UTC
Read the original article Hit count: 152

Filed under:
|
|

basically i want to hold a parameter that retrieve value from $.post() call like this:

init = function(){          
var lastpage = getLastPage();
}

        function getLastPage(){
            $.post("getInfo.php",{ 
                last: "yes"
            },
            function(data){
                setLast(data.last);
            },'json');

            return function setLast(data){
                return data;
            }
        }

so when reach at last post (last page) i should check with lastpage variable that has a value returned from getLastPage() function.

I'm pretty blur with javascript pointer and all. Please help guys.

update (20/4/2010):

I've done the other way around, like this:

init = function(){            
  getLastPage();
  if((page+1) == $("#lastpage").val()){
     alert("this is last post");
  }else{
     page++;
     //get info and display to the page here
  }
}
     function getLastPage(){
        $.post("getInfo.php",{ 
            last: "yes"
        },
        function(data){
            $("#lastpage").val(data.last);
        },'json');
    }

first run the function to temporarily store the value in hidden input tag (lastpage) and then grab the value again to check it whenever i click forward button.

if you all have more appropriate way please tell me.

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery