I'm working on a like-system, and I managed to get it to work, but in order for me to get it to work properly, I need it to check if the status is liked, and if it is, then unlike it, if not, then like it.
Now, here's my Javascript:
$('span#like-button<? echo $status_id; ?>').on('click', function(){
var isLiked<?echo $status_id; ?> = '';
var postid<? echo $status_id; ?> = $('input#postid<? echo $status_id; ?>').val();
var userid<? echo $status_id; ?> = $('input#userid<? echo $status_id; ?>').val();
var toid<? echo $status_id; ?> = $('input#toid<? echo $status_id; ?>').val();
$.get('https://mysite/stuff/ajax/isliked.php', {postid: postid<?echo $status_id; ?>, userid: userid<?echo $status_id; ?>}, function(data){
isLiked<?echo $status_id; ?> = data;
console.log(data);
});
if(isLiked<?echo $status_id; ?> == "Yes"){
console.log('Unliking Status');
$.get('https://mysite/dash', {unlike: postid<? echo $status_id; ?>, userid: userid<? echo $status_id; ?>}, function(data){
document.getElementById('like-button<? echo $status_id; ?>').innerHTML = 'Like';
});
} else if(isLiked<?echo $status_id; ?> == ""){
console.log('Liking Status');
$.get('https://mysite/dash', {like: postid<? echo $status_id; ?>, userid: userid<? echo $status_id; ?>, toid: toid<? echo $status_id; ?>},function(data){
document.getElementById('like-button<? echo $status_id; ?>').innerHTML = 'Unlike';
});
} else {
console.log("Error.");
}
});
My issue is that it runs the if statement before it runs the $.get to check if the status is liked. So, to the if statement, isLiked == '', thus trying to like the status when it's already liked, instead of being able to click the unlike button to unlike the status.
So, what I need to do, is every time, on click, run the $.get() part where it checks my isliked.php script, THEN run the if() statement to check the isLiked variable.
Thanks.
jquery.gethas asuccesscallback.