I am trying to get the value returned from a JavaScript function.
Here is the function:
function db_check(field_id, data_type, value) {
$.ajax({
url: 'ajax-php.php',
type: 'post',
dataType : 'json',
data: { 'db_check': 'true', 'field_id': field_id, 'value': value, 'data_type': data_type },
success: function (data, status) {
alert(data);
return data;
},
error: function (xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
}
}
While the function is running the alert(data);
return the good data. However if I use it somewhere within another function like Ex;
var result = db_check(field_id,data_type,value);
alert(result);
Then the alert is not showing the data but instead returning undefined.
I think this has to do with the call back. If I understand correctly, its because the alert(result); is getting executed before I get the data from db_check() function.
Could anyone show me how to get this working please.
EDIT
I know this is a duplicate but I have hard time understanding what seams to be a very good explanation because they use the different terms like foo,result,callback over and over so I am having hard time to figure what they stand for! Anyways i will keep trying i will eventually get it.