简体   繁体   中英

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. 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.

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).

(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.

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. I'm also unsure of the correct syntax needed to handle such an array on the PHP side to perform a simple Insert statement.

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' ? Check in console, paste the error.

In the php file you should have something a die or an echo to check if you enter.

Tell us something about the errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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