0

I was wondering something. I'm currently making a website and I have a function where you can post things on a stream and where you can like the stream.

Now I'm wondering, I have this in a .js file:

$(function(){
        $('.icon-like').click(function(){
                var count = parseInt($(this).html());
                count++;
                $(this).html(count++);
            });
});

What I want is everytime I click on the like icon, the user has no access to like it again (preventing a like-spam) and the row in the database will be updated.

However, I have no clue how to link PHP with JS. I know the PHP script I just don't know how to get it working.

Thanks already for helping ;)

Joshua Bakker
  • 101
  • 1
  • 4
  • 10
  • 2
    Read: http://stackoverflow.com/questions/7016701/creating-jquery-ajax-requests-to-a-php-function/7016795#7016795 – NullUserException Mar 28 '14 at 17:09
  • Use AJAX ($.ajax() or $.post()) to call the php script. If you need to return data, echo it in the php script. If you're returning an array of data, be sure to JSON encode the php array. – Jonathan Eltgroth Mar 28 '14 at 17:21

1 Answers1

2

Check this link http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php, it's a good start to learn how to use PHP with Javascript.

$('.icon-like').click(function(){        
    $.ajax({
        url: 'php/your-php-script-to-like-post.php',
        type: 'post',
        data: {...},
        success: function(data, status) {
           ...
           $(this).html(*updated value*);
        },
        error: function(xhr, desc, err) {
           ...      
        }
    }); 
});
Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44