Question

I have created a Facebook PHP app, and I tried to make a request dialog. I used the SDK, but it doesn't appear when I click on Invite Friends.

This is the code:

    <?php
// PATH TO THE FB-PHP-SDK
require_once 'src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '475970839112689',
  'secret' => '4ae240ac95f687af581ff1596d06121d'
));

$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();

if ( empty($user) ) {
    echo("<script> top.location.href='" . $loginUrl . "'</script>");
    exit();
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
etc...

After that, i loaded the JS for dialog and created a link:

    <div id="fb-root"></div>

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '475970839161689',
        status: true,
        cookie: true,
        oauth: true
    });
};

$('a').click(sendRequest);
function sendRequest() {
    FB.ui({
        method: 'apprequests',
        message: 'Vino si soializeaza si tu pe Social Chat!',
        title: 'Trimite prietenilor tai o invitatie',
    },
    function (response) {
        if (response.request && response.to) {
            var request_ids = [];
            for(i=0; i<response.to.length; i++) {
                var temp = response.request + '_' + response.to[i];
                request_ids.push(temp);
            }
            var requests = request_ids.join(',');
            $.post('handle_requests.php',{uid: <?php echo $user; ?>, request_ids: requests},function(resp) {
                // callback after storing the requests
            });
        } else {
            alert('canceled');
        }
    });
    return false;
}

  // Load the SDK Asynchronously
(function(d){
 var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "//connect.facebook.net/ro_RO/all.js";
 d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>

<a href="#">Invite friends</a>

So, when I click on Invite friends link, it doesn't do anything.

Was it helpful?

Solution

Just wrap your click function in ready function like this :

$(document).ready(function() { 
   $('a').click(sendRequest);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top