doutangtan6386 2013-05-29 18:29
浏览 42
已采纳

用户在Facebook上共享内容后插入数据库

I am working on a Facebook tap app. In the app the user selects an item page and in there he can write a comment about the item. After he has written his comment he clicks a button that opens up a facebook share dialog and the user comment is in the dialog box.

What I want to accomplish is inserting the comment to a database when the user clicks share and if he clicks cancel nothing happens.

Here is the function that I use to open up the dialog:

function FacebookPostToWall()
    {
    var comment = document.getElementById('comment').value;;
    FB.ui({
        method: 'feed',  
        link: 'http://linkfortheitem.com',
        name: "Name of the item",
        caption: "Caption for the item",
        description: '' + comment,
        picture: '',
        message: ''
        },
        function(response){
            if(response && response.post_id) {
                alert('user has shared');
            }else {
                alert('user has not shared');
            }       
    });
    }

So my question is, is there a way to call my php function that inserts the comment to database inside the callback function?

  • 写回答

1条回答 默认 最新

  • duanmu5039 2013-05-29 18:53
    关注

    You can use ajax to send the comment to a php script that can retrieve the parameter via $_POST['comment'] (or whatever you decide to call the parameter). If you're using plain old javascript, first example shows you something that would use to send an ajax request with your comment variable. The second example which is ridiculously easier uses jQuery, which is most definitely worth learning. Both of these would go immediately after (or replace) alert('user has shared'); in your script, so that the call is sent only if the facebook comment was successfully posted.

    Let me know if this was what you were looking for and if you have any questions :)

    Javascript example

    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert('Comment sent to PHP, and the response is: '+xmlhttp.responseText);
            }
        });
    xmlhttp.open("POST","yourphpscript.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("comment="+comment);
    

    jQuery Example

    $.post('yourphpscript.php', { 'comment': comment }, function(response) {
            alert('Comment sent to PHP, and the response is: '+response);
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭