Joshua Bakker
Joshua Bakker

Reputation: 99

PHP code in JavaScript

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 ;)

Upvotes: -1

Views: 65

Answers (1)

Jonathan Anctil
Jonathan Anctil

Reputation: 1037

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) {
           ...      
        }
    }); 
});

Upvotes: 2

Related Questions