简体   繁体   English

Facebook-如何通过Ajax POST将用户ID数组发送到PHP文件

[英]Facebook - How to send an array of User IDs to a PHP file by Ajax POST

Hello_I'm currently creating an Invitation & Reward system for my Facebook Canvas App. 您好:我目前正在为我的Facebook Canvas应用创建邀请和奖励系统。 Right now I have a working Multi-Friend Selector where users can select their friends from a list, and when they click the "Invite" button this code is run: 现在,我有一个工作的多好友选择器,用户可以在其中从列表中选择他们的朋友,当他们单击“邀请”按钮时,将运行以下代码:

   function sendRequest() {
            // Getting the list of selected friends
            var sendUIDs = '';
            var mfsForm = document.getElementById('mfsForm');
            for (var i = 0; i < mfsForm.friends.length; i++) {
                if (mfsForm.friends[i].checked) {
                    sendUIDs += mfsForm.friends[i].value + ',';
                }
            }

        // Using FB.ui to send the Request(s)
        FB.ui({
            method: 'apprequests',
            to: sendUIDs,
            title: 'My App Invite',
            message: 'Try my App! :)',
        }, 
            console.log(sendUIDs);
            callback);

////// My attempt at POSTing the sendUIDs array:

        dataString = sendUIDs; 
        var jsonString = JSON.stringify(dataString);

   $.ajax({
        type: "POST",
        url: "InsertRecipientIDs.php",
        data: {data : jsonString}, 
        cache: false,

        success: function(){
            alert("OK");
        }
    });   
   }

This code sends Invites without any problems. 此代码可以毫无问题地发送邀请。 However, I would also like to be able to reward the invite sender whenever an invited person begins using the App. 但是,每当受邀人开始使用该应用程序时,我也希望能够奖励邀请发件人。

Since sendUIDs is an Array/list of the IDs of everyone who receives an invite, I want to grab them, POST them by AJAX to a PHP file and then Insert each ID into a row in my database, along with the invite sender's ID. 由于sendUIDs是接收邀请的每个人的ID的数组/列表,因此我想抓取它们,通过AJAX将其发布到PHP文件,然后将每个ID以及邀请发送者的ID插入到我的数据库行中。

I'll then cross reference this list with the ID of any new users to the App to apply a reward to the invite sender(s). 然后,我将将此列表与该应用程序的任何新用户的ID进行交叉引用,以将奖励应用于邀请发件人。

(Originally I was trying to perform this inviter/invitee check by querying the Graph API for a list of invite senders whenever a new user begins using the App, but I ran into difficulties, please see here: POSTing an Array from a Facebook Graph API response by AJAX to a PHP script So I've been trying to work around it using this method instead. (最初,当新用户开始使用该应用程序时,我试图通过查询Graph API来查询邀请发件人列表,从而尝试执行此邀请者/被邀请者检查,但是遇到了麻烦,请参阅此处: 从Facebook Graph API发布数组AJAX对PHP脚本的响应因此,我一直在尝试使用此方法来解决它。

The AJAX at the end of this code is what I've been trying in order to format the Array correctly for database insertion, but it's not working. 这段代码末尾的AJAX是我一直在尝试的格式,以便正确格式化Array以进行数据库插入,但是它不起作用。 I'm also unsure of the correct syntax needed to handle such an array on the PHP side to perform a simple Insert statement. 我也不确定在PHP端处理这样的数组以执行简单的Insert语句所需的正确语法。

Any help at all with this, or my other question would be hugely appreciated! 任何与此有关的帮助,或者我的其他问题将不胜感激!

Thank you in advance! 先感谢您!

Have you checked if you enter in 'InsertRecipientIDs.php' ? 您是否检查了是否输入“ InsertRecipientIDs.php”? Check in console, paste the error. 在控制台中签入,粘贴错误。

In the php file you should have something a die or an echo to check if you enter. 在php文件中,您应该有一个死角或回声来检查是否输入。

Tell us something about the errors. 告诉我们一些有关错误的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM