Running a MySQL Query when Facebook Like Button is Clicked
I would like to add an entry into a MySQL table called fblikes
every time a user likes a something on my site. According to this page, you should be able to do this, and "edge.create开发者_JS百科" is "fired" when someone likes something.
What I want to do is run the MySQL query below when "edge.create" is "fired."
How do I do this?
FB.Event.subscribe('edge.create', function(response)
{
mysql_query("INSERT INTO fblikes VALUES (NULL, '$submissionid', '$uid', NULL)");
});
EDIT: I am using the code below for the like button, and it appears to me to be including some sort of javascript library.
echo '<div id="fb-root"></div>';
echo "<script>
window.fbAsyncInit = function() {
FB.init({appId: 'your app id', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
echo '</script>";
You are mixing javascript and php. What you need to do is send an ajax request to the server in the callback of the subscribe
function.
On the server side you need to have a php script that processes your sent-in variables and adds them to the database.
Are you using any kind of javascript library?
FB.Event.subscribe <-- is javascript, so php won't work here.
What I would do is do a jquery post to a php page which does the mysql_query for you.
Put the jquery post inside the subscribe function.
精彩评论