0

i am using facebook api to populate a from, it seems work perfectly, the only problem is that i have the form populated only when i refresh the page, there is a way to poulate immedialty? here my code:

HTML:

<li><a href="javascript:void();" onclick="fb_login();"><img src="img/fb_login.png" border="0" alt=""></a></li>

JAVASCRIPT

          function statusChangeCallback(response) {
            console.log('statusChangeCallback');
            console.log(response);
            if (response.status === 'connected') {
              testAPI();
            } else if (response.status === 'not_authorized') {
              console.log ('non autorizzato.');
            } else {
              console.log('accedi a facebook');
            }
          }

          function fb_login(){
              FB.login(function(response) {

                  if (response.authResponse) {
                      console.log('Welcome!  Fetching your information.... ');
                      access_token = response.authResponse.accessToken; //get access token
                      user_id = response.authResponse.userID; //get FB UID

                    console.log('access_token: '+access_token+", user_id: "+user_id);

                      FB.api('/me', function(response) {
                          user_email = response.email; //get user email
                      });

                  } else {
                      //user hit cancel button
                      console.log('User cancelled login or did not fully authorize.');

                  }
              }, {
                  scope: 'public_profile,email'
              });
          }
          function checkLoginState() {
            FB.getLoginStatus(function(response) {
              statusChangeCallback(response);
            });
          }


          // ID APPLICAZIONE
          window.fbAsyncInit = function() {
            FB.init({
              appId      : 'myid', //SVILUPPO
              xfbml      : true,
              version    : 'v2.5'
            });

              FB.getLoginStatus(function(response) {
                statusChangeCallback(response);
              });
          };

        // Carico la SDK in asincrono
          (function(d, s, id){
             var js, fjs = d.getElementsByTagName(s)[0];
             if (d.getElementById(id)) {return;}
             js = d.createElement(s); js.id = id;
             js.src = "//connect.facebook.net/en_US/sdk.js";
             fjs.parentNode.insertBefore(js, fjs);
           }(document, 'script', 'facebook-jssdk'));

        //compilo i campi
          function testAPI() {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', {fields:'email,first_name,last_name'}, function(response) {
            console.log(response);
              console.log('Successful login for: ' + response.first_name);
              fb_populate(response)
            });
          }

          function fb_populate(response){
              $('#social').val('facebook_'+response.id);
              $('#nome').val(response.first_name);
              $('#cognome').val(response.last_name);
              $('#email').val(response.email);
          }

1 Answer 1

0

My answer is maybe a little late, but you can try to use the FB.XFBML.parse(); function from the JavaScript SDK.

See: How to force Facebook JS-SDK to render dynamically added widgets?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.