Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML

I want to use javascript sdk because facebook php sdk new version doesnot support my company's PHP version.
Ok so... I want to insert the email of FacebookUser to database MySQL. Please look at the fucntion get_Info(). I want the value of "response.email" parse to PHP variable and insert into MySQL, but I don't know how to do because this variable is in the js function

What I have tried:

JavaScript
		 window.fbAsyncInit = function() {

  FB.init({
    appId: 'xxxxxxxx',  //Your appId
    xfbml: true,
    version: 'v2.8'
  });
  FB.AppEvents.logPageView();
  FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
      document.getElementById('status').innerHTML = 'We are connected.';
      document.getElementById('fb_login').style.visibility = 'hidden';
    } else if (response.status === 'not_authorized') {
      document.getElementById('status').innerHTML = 'We are not logged in.';
    } else {
      document.getElementById('status').innerHTML = 'You are not logged into Facebook';
    }
  });
};

(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'));

function fb_login() {
  FB.login(function(response) {
    if (response.status === 'connected') {
      document.getElementById('status').innerHTML = 'We are connected.';
      document.getElementById('fb_login').style.visibility = 'hidden';
    } else if (response.status === 'not_authorized') {
      document.getElementById('status').innerHTML = 'We are not logged in.';
    } else {
      document.getElementById('status').innerHTML = 'You are not logged into Facebook';
    }
  }, {
    scope: 'email'
  });
}

function getInfo() {
  FB.api('/me', 'GET', {
    fields: 'first_name,last_name,name,id,email'
  }, function(response) {
    var email = response.email;       /////////variable that I want to send to server-side ///////////

    var http = new XMLHttpRequest();      ///////////AJAX///////
    var url = "new 1.php";
    var data = "email=" + email;
    http.open("GET", url, true);    
    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    } 
    http.send(data);
  });
}

function fb_logout() {
  FB.logout(function() {
    document.location.reload();
  });
}


HTML body
<div id="status"></div> <div id="status2"></div>
<button onclick="getInfo()"> Get Info</button>
<button onclick="fb_login()" id="fb_login"> Login with Facebook </button>
<button onclick="fb_logout()"> Log out </button>



in new 1.php

<?php 
if($_SERVER['REQUEST_METHOD'] == 'GET'){
    $email = $_GET['email'];
    echo $email;
}
?>
Posted
Updated 2-Nov-16 4:18am
v12

1 solution

You need to get your data back to the server either via:

1) A <form> to a php function on server for database access
2) AJAX (javaScript on client side->php on server side) for database access

These have nothing to do with Facebook SDK, or any other - they're part of how you use javaScript and php .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900