0

I am trying to individually track the number of people who liked my facebook page from a particular webpage. For that I am using FB event. I wish to save the counts to my local database.

Below is the code that I have been trying out. But it is not working.

<?php
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:fb=\"http://    www.facebook.com/2008/fbml\">";

echo "<head>";
echo "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js\"></script>";
echo "<script src=\"http://connect.facebook.net/en_US/all.js#appId=157605600977665&amp;xfbml=1\"></script>";
echo "<script>";

echo "var a = 0;";
echo "$(\"document\").ready(function(){";
echo "FB.Event.subscribe('edge.create', function(href, widget) {";
//not working
echo "window.location=\"1.php?action=add";
echo "});";
echo "});";
echo "</script>";
echo "</head>";

echo "<body>";

if ($action=="add") 
{ 
mysql_query("update likes set count = count + 1 where id=1;");
} 

echo "<div id=\"fb-root\"></div>";
echo "<fb:like href=\"http://www.facebook.com\" send=\"false\" width=\"450\" show_faces=\"false\"></fb:like> <br/>";
echo "</body>";
echo "</html>";

?>
2
  • What doesnt work ? does the event get fired ? Commented Nov 17, 2011 at 9:41
  • "window.location=\"1.php?action=add"; <- thats not correct the string passed to window.location should be enclosed by quotes ie you missed the closing quote Commented Nov 17, 2011 at 9:43

1 Answer 1

1

You want to use the $_GET variable which will contain any url parameters.

if (isset($_GET['action']) {
   $action = $_GET['action'];
   if ($action == "add") {
     mysql_query("update likes set count = count + 1 where id=1;");
   } 
} // untested, should work though

Also by the way, you don't have to echo out all your HTML. You could open and close the PHP tags anytime you want.

<html>
<head>
....etc...

<?php
  // do work here
?>

.... more html ...
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for quick replies. I edited as you had mentioned. But, there is parse error. Parse error: syntax error, unexpected '{' in C:\wamp\www\Counter\1.php on line 28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.