Update database field on Facebook "Like"
I'd like to track when people click the Facebook "Like" button on my website.
I have a small script set-up, but it doesn't seem to work and I'm out of ideas of what it could be. Any suggestions? The AppID is correct and this script is just for testing so don't mind the lack of validation:
index.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>FB Like Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
$(document).ready(function() {
window.fbAsyncInit = function() {
FB.init({appId: 'x', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('edge.create', function(href, widget) {
$.ajax({
type: 'POST',
url: 'like-sql.php',
data: ({liked : 1})
});
});
FB.Event.subscribe('edge.remove', function(response) {
$.ajax({
type: 'POST',
url: 'like-sql.php',
data: ({liked : 0})
});
});
};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = 'http://connect.facebook.net/nl_NL/all.js#appId=x&xfbml=1';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
});
</script>
<fb:like href="http://x.x.x" layout="button_count" show_faces="true" width="500"></fb:like>
</body>
</html>
like-sql.php
<?php
$status = $_POST['liked'];
mysql_query("UPDATE `开发者_如何学运维fb_like` SET umk_like = $status WHERE user_id = '3432'");
?>
I'm new to this whole jQuery ajax thingy, so I don't really know how I should debug this. Any suggestions are welcome :)
EDIT: Nevermind guys I got it.
I'm testing this in an external file, where I don't have jQuery included by default ;) I can't believe it took me this long to figure it out, lol. Thanks anyway for all the help!
Try to alert() something in the event listener itself, to see if it is subscribed correctly
FB.Event.subscribe('edge.create', function(href, widget) {
alert('Like caught !');
$.ajax({
type: 'POST',
url: 'like-sql.php',
data: ({liked : 1})
});
});
and add notify=true to fb:like
<fb:like notify="true" href="http://x.x.x" layout="button_count" show_faces="true" width="500"></fb:like>
I had the same problem with "comment.create", and I solved it by notify="true", and subscribing to "comments.add" ! but I still can't subscribe to "comment.remove"!
Nevermind guys I got it.
I'm testing this in an external file, where I don't have jQuery included by default ;) I can't believe it took me this long to figure it out, lol. Thanks for all the help though!
精彩评论