Last Updated: February 25, 2016
·
969
· v00d0

[FACEBOOK] GET USER PERMISSION AND SAVA DATA VIA AJAX

What this code does:
1) after click, check if user is logged in FB
2) if it doesn't ask to log in
3) after login, it ask permission
4) if user accept it send gathered data to pass_value.php via Ajax and it will redirect the user to an other page called gosomewhere.php

// always put this function inside a click or a user action, don't automatically open it or browser will block the popup. 
               $('#button_id').click(function(){ 
                    FB.login(function(logged) {
                       if(logged.status == "connected"){
                           FB.api("/me", function(response) {
                               $.ajax({
                                   type: "POST",
                                   url: "pass_value.php",
                                   data: {fb_id: response.id, name: response.first_name, last_name: response.last_name, gender: response.gender},
                                   success: function(){window.location.href = "gosomewhere.php"; }
                               }); 
                           });  
                       }
                      /* in the scope u specify all permission you need, in this case you get all basic INFO and pubblish to wall permission */
                   }, {scope: 'publish_stream'});  

               });

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside