Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Return value undefined from $.post

5:17 am on Apr 22, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 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.
5:51 am on Apr 22, 2012 (gmt 0)

New User

joined:Apr 15, 2012
votes: 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!
4:21 am on Apr 23, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 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!

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members