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);
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路