I wants to get the last id from MySQL table and create a cookie with this value in Javascript and PHP. following is my code.
if (getCookie("id")=='') {
    last_visitor_id = get_last_visitor();
    deviceID = last_visitor_id++;
    setCookie("id", deviceID , 365);
    $.ajax({
        url:"save_device.php?deviceID="+deviceID+
            "&websiteID="+WebsiteID+
            "&width="+sWidth+
            "&height="+sHeight,
            success:function (datasource) {
                // alert (datasource);
            }
        }
    );  
} else {
    deviceID = getCookie("id");
    alert ("working...." + deviceID);
}
 function get_last_visitor(){
    $.ajax({url:"get_last_visitor.php",success:function(last_visitor){
         // last_visitor++;
         //alert ("Last Visitor ID " + last_visitor);
         return (last_visitor);
    }});        
}
There is a problem with code. Before it gets the last visitor id it executes the setCookie function even though there is no value returned by the get_last_visitor() function. Functions return the value after some time, but in the mean time it creates a cookie with NAN.
Can any one suggest some solution?
 
     
    