| Return value undefined from $.post
|
neophyte

msg:4443877 | 5:17 am on Apr 22, 2012 (gmt 0) | Hello All - While I've been coding php for years, I'm a real noob at javascript/jquery and have been beating my head over this single function for almost 2 days. I've cobbled together the valAgainstDataBase() function (shown below) from an example found online. All I need is a return value of TRUE OR FALSE which is echoed from ajax_works_v2.php. However, all that I get is from my calling function is "undefined" Here's the calling function: function checkLoginForm() { var logn = valAgainstDataBase(); alert('return value: ' + logn); } Here's the function BEING CALLED that should return TRUE or FALSE: function valAgainstDataBase() { $.post("ajax_works_v2.php", {username: $('[id=username]').val(), password: $('[id=password]').val()}, function(data) { return data; }) } The .php file is echoing correctly so I know the issue lies somewhere in valAgainstDataBase(). Can someone please show me the error of my ways? Great appreciation to everyone in advance.
|
shingokko

msg:4443881 | 5:51 am on Apr 22, 2012 (gmt 0) | OK, the problem is that the callback function of the AJAX call is inside valAgainstDataBase thus the value returned from the function is not used. I think it would work if you change it to:
function valAgainstDataBase(callback) { $.post("ajax_works_v2.php", { username: $('[id=username]').val(), password: $('[id=password]').val()}, callback); }
function chestLoginForm() { valAgainstDataBase(function(data) { alert('return value: ' + data); }); } Hope this works!
|
neophyte

msg:4444092 | 4:21 am on Apr 23, 2012 (gmt 0) | Hey shingokko - Thanks very much for your reply - indeed your first post in WebmasterWorld! While your solution worked, it didn't work as I really needed it to (I need checkLoginForm to return the value of valAgainstDataBase to a prevoius function). But it's okay - found a workaround that (while more verbose) does satisfy my need. Thanks again!
|
|
|