开发者

Using Ajax to Run a MySQL Query via PHP When Facebook Like Button is Clicked

开发者 https://www.devze.com 2023-03-13 09:12 出处:网络
I would like to run a MySQL query each time a Facebook Like button is clicked on a page.I already know that FB.Event.subscribe(\'edge.create\', function(response) {} is used to exe开发者_StackOverflow

I would like to run a MySQL query each time a Facebook Like button is clicked on a page. I already know that FB.Event.subscribe('edge.create', function(response) {} is used to exe开发者_StackOverflow社区cute something when a Like button is clicked. My problem is that I don't know Javascript / AJAX. What simple Javascript/AJAX code that I can but in the curly brackets of the FB.event that will just run the MySQL query below? My understanding is that I might need to get some sort of JQuery library, which is fine.

Thanks in advance,

John

The relevant code on the page:

<script src="http://connect.facebook.net/en_US/all.js"></script>

<?php


session_start();

$uid = $_SESSION['loginid'];

$_SESSION['submissionid'] = $submissionid;

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

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

echo '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="" send="true" layout="button_count" width="450" show_faces="false" font="arial"></fb:like>';

The MySQL query that I would like to run each time a Like button is clicked:

mysql_query("INSERT INTO fblikes VALUES (NULL, '$submissionid', '$uid', NULL)");


(1) Add onclick action calling updateMe(), to the button.

(2) *Create a function updateMe()

function updateMe() {
    $.post("/path/to/your/updatefile.php", {sid: Math.random()}, function(){});
}

(3) Create a file "updatefile.php" which holds and execute your mysql query.

  • I saw you save details in session, so I didn't give an example how to pass them via the updateMe() function.


Using jQuery

$.ajax({
   url: "test.html", // the url of your php script
   context: document.body,
   success: function(){
      // if you want something to be executed when a result comes back
   }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消