本文介绍了用户在Facebook上共享内容后插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Facebook上点击应用程序。在应用程序中,用户选择了一个项目页面,在那里他可以写出关于该项目的评论。在他写了他的评论后,他点击一个打开一个Facebook共享对话框的按钮,用户评论在对话框。



我想要完成的是插入在用户点击共享时注释数据库,如果他点击取消没有任何反应。



这是我用来打开对话框的功能:

 函数FacebookPostToWall()
{
var comment = document.getElementById('comment')。
FB.ui({
method:'feed',
link:'http://linkfortheitem.com',
名称:项目名称,
caption:Caption for the item,
description:''+ comment,
picture:'',
message:''
},
function (回复){
if(response&& response.post_id){
alert('user has shared');
} else {
alert('user not not共享');
}
});
}

所以我的问题是,有没有办法调用我的php函数插入回调函数中的数据库注释?

解决方案

您可以使用将注释发送到可通过 $ _ POST ['comment'] (或任何你决定调用参数)。如果您使用简单的旧版JavaScript,第一个示例会显示一些用于将您的注释变量发送给ajax请求的内容。第二个例子很容易使用,这绝对值得学习。在脚本中(或替换) alert('user has shared'); 之后,这两个都将立即执行,以便只有在facebook注释成功时才会发送呼叫发布。



让我知道这是你正在寻找的,如果你有任何问题:)



Javascript示例



  var xmlhttp; 
if(window.XMLHttpRequest)
{
//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{
// IE6,IE5的代码
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
alert ('发送给PHP的评论,响应是:'+ xmlhttp.responseText);
}
});
xmlhttp.open(POST,yourphpscript.php,true);
xmlhttp.setRequestHeader(Content-type,application / x-www-form-urlencoded);
xmlhttp.send(comment =+ comment);



jQuery示例



 code> $。post('yourphpscript.php',{'comment':comment},function(response){
alert('发送给PHP的评论,响应是:'+ response);
});


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?

解决方案

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);
    });

这篇关于用户在Facebook上共享内容后插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:35