I am working on a project where i post "thumbsup" and "like" click via AJAX/POST on a PHP Script. This returns after processing, depending if the user is logged in or not an error array (with json_encode). 0 means its all ok, 1 means the user was not logged in. The submit function i have written does not return the error variable after redefinig it on each loop. When i do console.log(error) on each loop it does return 1, but when i check it on click function it returns false. I have the following 2 functions:
I cant seem to understand what i am doing wrong.
function submit(tip,varid){
var error = false;
$.post( "/rwfr.php", { name: ""+tip+"", id: ""+varid+"" })
.done(function( data ) {
var results = jQuery.parseJSON(data);
$(results).each(function(key, value) {
error = value['error'];
return false;
})
});
return error;
}
$(".fa-thumbs-up").click(function(){
var idObj = $(this).parent().parent().attr("data-id");
var act = submit('thumbsup',idObj);
if(act == "1"){
console.log(act);
alert("You must log in before you can rate this video!");
}
});