2011-06-17 93 views
0

我想每次在頁面上點擊Facebook Like按鈕時運行MySQL查詢。我已經知道FB.Event.subscribe('edge.create', function(response) {}被用來執行點擊Like按鈕時的操作。我的問題是,我不知道Javascript/AJAX。我可以使用哪些簡單的Javascript/AJAX代碼,但是隻在FB.event的大括號內運行下面的MySQL查詢?我的理解是,我可能需要獲得某種JQuery庫,這很好。當點擊Facebook Like按鈕時,使用Ajax通過PHP運行MySQL查詢

由於提前,

約翰

頁面上的相關代碼:

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

MySQL查詢我想每一個Like按鈕被點擊時運行:

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

回答

0

(1)添加onclick動作,調用updateMe(),到按鈕。

(2)*創建一個功能updateMe()

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

(3)創建一個文件 「updatefile.php」 持有並執行你的MySQL查詢。

  • 我看到你保存會話中的細節,所以我沒有舉例說明如何通過updateMe()函數傳遞它們。
+0

謝謝...我在哪裏把這個updateMe()函數? – John 2011-06-17 00:46:55

+0

''script type =「text/javascript」> function ...'它應該和當然按鈕在同一個文件中。 – RRStoyanov 2011-06-17 00:54:22

+0

這將與http://connect.facebook.net/en_US/all.js庫一起工作嗎? – John 2011-06-17 01:06:46

0

使用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

謝謝......我把這個放在FB.Event.subscribe('edge.create',function(response){}的花括號內嗎? – John 2011-06-17 00:54:08

+0

是的 - 希望這個適合你! – Ord 2011-06-17 01:00:21

+0

將一個庫叫做jquery.pack .js可能適用於此? – John 2011-06-17 01:06:26