facebook javascript SDK-如何将用户数据插入数据库

facebook javascript SDK-如何将用户数据插入数据库,javascript,php,mysql,facebook,api,Javascript,Php,Mysql,Facebook,Api,我想使用javascript sdk,因为facebook php sdk新版本不支持我公司的php版本。 好吧,那么。。。我想将FacebookUser的电子邮件插入数据库MySQL。 请看功能获取信息()。我想把“response.email”的值解析成PHP变量并插入MySQL,但我不知道怎么做,因为这个变量在js函数中 window.fbAsyncInit = function() { FB.init({ appId: 'xxxxxxxxxxxx', //Your a

我想使用javascript sdk,因为facebook php sdk新版本不支持我公司的php版本。
好吧,那么。。。我想将FacebookUser的电子邮件插入数据库MySQL。 请看功能获取信息()。我想把“response.email”的值解析成PHP变量并插入MySQL,但我不知道怎么做,因为这个变量在js函数中

  window.fbAsyncInit = function() {

  FB.init({
    appId: 'xxxxxxxxxxxx',  //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) {
    document.getElementById('status').innerHTML = response.email;
  });

  FB.api('/me', 'GET', {
    fields: 'first_name,last_name,name,id,email'
  }, function(response) {
    document.getElementById('status2').innerHTML = response.name;
  });
}

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


显示简介
使用Facebook登录
注销

为此,您必须使用AJAX。如果您想将JS数据传递给PHP,可以调用AJAX。例如,您可以这样做:

1.修改
getInfo()
函数如下: 2.创建
文件.php
并编写以下代码:




代码的作用是使用简单的AJAX调用将JS数据发送到PHP文件。在
文件.php
文件中,您可以对数据执行任何操作。在您的情况下,它将保存一封电子邮件到数据库。我希望这会有所帮助,你必须使用AJAX。如果您想将JS数据传递给PHP,可以调用AJAX。例如,您可以这样做:

1.修改
getInfo()
函数如下: 2.创建
文件.php
并编写以下代码:




代码的作用是使用简单的AJAX调用将JS数据发送到PHP文件。在
文件.php
文件中,您可以对数据执行任何操作。在您的情况下,它将保存一封电子邮件到数据库。我希望它会有所帮助

,运行此代码时会发生什么?您是否在控制台(F12->Network)中检查了您的请求发生了什么情况?浏览器控制台中没有任何错误,但第3行的otherpage.php的mypath中有您的代码警报:未定义索引:电子邮件。我的另一个page.php看起来像您的“file.php”,只有7行代码尝试将请求方法修改为
GET
。在
getInfo()
函数中,将行更改为
http.open(“GET”,url,true)
并在otherpage.php文件中,将代码更改为
如果($\u SERVER['REQUEST\u METHOD']='GET'){$email=$\u GET['email'];echo$email;//用电子邮件做任何你想做的事}
不再工作,但现在,在other page.php中,第3行的C:\xampp\htdocs\new 1.php中有错误消息通知:未定义索引:电子邮件。(之前只看到此警报)您的数据发送是否正确?转到按钮所在的页面,点击[F12]打开控制台并转到网络选项卡。然后单击按钮,查看请求的状态是否为OK,以及运行此代码时会发生什么情况?您是否在控制台(F12->Network)中检查了您的请求发生了什么情况?浏览器控制台中没有任何错误,但第3行的otherpage.php的mypath中有您的代码警报:未定义索引:电子邮件。我的另一个page.php看起来像您的“file.php”,只有7行代码尝试将请求方法修改为
GET
。在
getInfo()
函数中,将行更改为
http.open(“GET”,url,true)
并在otherpage.php文件中,将代码更改为
如果($\u SERVER['REQUEST\u METHOD']='GET'){$email=$\u GET['email'];echo$email;//用电子邮件做任何你想做的事}
不再工作,但现在,在other page.php中,第3行的C:\xampp\htdocs\new 1.php中有错误消息通知:未定义索引:电子邮件。(之前只看到此警报)您的数据发送是否正确?转到按钮所在的页面,点击[F12]打开控制台并转到网络选项卡。然后单击按钮,查看您的请求状态是否正常
<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> 
function getInfo() {
  FB.api('/me', 'GET', {
    fields: 'first_name,last_name,name,id,email'
  }, function(response) {
    var email = response.email;

    var http = new XMLHttpRequest();
    var url = "path/to/your/file.php";
    var data = "email=" + email;
    http.open("POST", 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);
  });
}
<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $email = $_POST['email'];
    echo $email;
    //Do whatever you want with your email
}
?>