Custom likes counter with jquery

I want to make a custom likes counter. For this I have used the following code:

var likes = 0;
$('#button').click(function(){
      likes += 1;
      $('#likes').text(likes); //A <p id="likes"> html tag created before
      window.stop(likes);
});

The problem is the scope of this function. I need to use the "likes" variable outside the scope to convert it to a php variable and save that value in my mysql database. I've tried several ways, but it has not worked for me. Any suggestions?

Upvotes: 0

Views: 879

Answers (1)

Patrick Simard
Patrick Simard

Reputation: 2375

You just need the ajax part to save it

var likes = 0;
$('#button').click(function(){
    likes += 1;

    $.ajax({
        type: "POST", 
        url: "/save.php", 
        data: "likes="+likes, 
        success: function(returnMsg){ 
          $('#likes').text(likes);
          window.stop(likes);
        }
    });
});

As for the PHP well the var will be $_POST['likes'] use it to save it

Upvotes: 1

Related Questions