0

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!");
    }
});
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Florin Andrei
  • 287
  • 2
  • 11
  • 1
    Remove `return false;` in `each` loop. – Oen44 Apr 12 '17 at 19:06
  • See also [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – James Thorpe Apr 12 '17 at 19:14
  • You know that the submit function returns long before the .done function gets called right? – James Apr 12 '17 at 19:15

1 Answers1

-2

Though it is down voted, I've kept this answer as someone with similar problem may find this simple and helpful.

The reason it's returning false in your case is that, by the time you get the response from submit(), your next line i.e. if(act=="1") is being run, which will of course return false since you've initialized error with vallue false.

What you can change in your function is that, you may just move your snippet that checks the error in response received inside a callback function which must be called within .done() of your post request.

See below,

// your submit function
function submit(tip, varid, callback){
    var response = $.post("post.php", { name: ""+tip+"", id: ""+varid+""});
    response.done(function(data){
        callback(JSON.parse(data), tip);
    });
}

// your callback function
function callbackFunction(response, action_type){
    // handle your prompts here based on your action_type i.e. thumbs up, down, favourite, etc.
    console.log(response);
    if(response.hasOwnProperty("error") && response["error"]=="1"){
        console.log("you need to login to do this!");
    }
}

// and pass the callback function to your submit function
var act = submit('thumbsup',idObj, callbackFunction);
Yogesh Mistry
  • 2,082
  • 15
  • 19
  • From what i know return false does break the each loop. I did tried it without return false, same result. – Florin Andrei Apr 12 '17 at 19:06
  • `return false` inside of jQuery's `.each` will stop the iteration. This would not be the case in the native `.forEach` or in lodash or underscore. This is a horrible design decision on their part in my opinion. – Charlie Martin Apr 12 '17 at 19:10
  • Thanks, this works without the each loop. but still what is wrong with my code? Is there any way to make it work like i wannted to in first place. I cannot use the errors output inside the submit function because i will use this function for more actions ( thumbs up, thumbs down, watch later, add to favorite, report) so every click action should have its own message. – Florin Andrei Apr 12 '17 at 19:15
  • okay. you may check the edit part of the answer now – Yogesh Mistry Apr 12 '17 at 19:44