Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill form using facebook data

I would like to allow users to press a button on my page that will autofill a form with their first name, last name, and e-mail address, provided that they are logged into Facebook. If they are not, then the Facebook login form should popup, allowing the user to login, and then the auto fill should take place.

I have been messing around with this in the javascript test console. I know I will need to call the first_name, last_name, and email, but I'm not sure how to populate my form fields with this information.

FB.api(
      {
        method: 'fql.query',
        query: 'SELECT first_name, last_name, email FROM user WHERE uid=' + response.authResponse.userID
      },

I would like to do this in javascript. I need to create a function that will populate a form with this information on click. Any ideas?

like image 331
nelle-f Avatar asked Oct 29 '12 15:10

nelle-f


People also ask

How do I get form data on Facebook?

To download leads data from Business Suite:Go to Meta Business Suite. Click All Tools from the menu. Click Instant Forms. Click Download next to the form with the leads you want to export.

How do I create a fillable form on Facebook?

To add a fillable form to a Facebook page takes 3 simple steps: Create a form. Set up a custom page on Facebook. Embed the form.


1 Answers

Why don't you use Registration Plugin. It will save you a lot of work https://developers.facebook.com/docs/plugins/registration/

In Case you want to have custom form. You would have to take permission from user and get his details. You can use following Code :

<div id="fb-root"></div> 
<script src="https://connect.facebook.net/en_US/all.js"></script> 
FB.init({appId: 'XXXXXXXXXXXXX',status : true,xfbml  : true, cookie: true ,oauth  : true});


function login(){
        FB.getLoginStatus(function(response) {
        // checks if user already authorized
                    if (response.status === 'connected') {
                            FB.api('me?fields=id,name,email', function(user) {
                                     if(user != null) {
                                                                username  = user.name;
                                                                uid = user.id;
                                                                email = user.email;
                                                            }
                                });

                    } else {
        // If user is not authorized call FB.login for pop up window for authorization  , scope defines list of persmission requested with user
                    FB.login(function(response) {
                            if (response.authResponse.accessToken) {    
                                            FB.api('me?fields=id,name,email', function(user) {
                                                    if(user != null) {
                                                                username  = user.name;
                                                                uid = user.id;
                                                                email = user.email;
                                                            }
                                                        }) 
                                                    } 
                                                }, {scope:'email'});
                    }

                });
    }

You call login() at the press of your button.

like image 104
Vishwesh Shetty Avatar answered Sep 28 '22 18:09

Vishwesh Shetty