homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Return value undefined from $.post
neophyte

10+ Year Member



 
Msg#: 4443875 posted 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#: 4443875 posted 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

10+ Year Member



 
Msg#: 4443875 posted 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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved