1

我試圖在用戶登錄後在數據庫中保存用戶詳細信息。我正在使用JavaScript SDK。我通過「onlogin」進行了回調。我認爲問題出在我的javascript代碼上。請參閱下面的我的代碼。無法在數據庫中保存Facebook登錄用戶的詳細信息

請幫忙!!

HTML:

<html> 
<head> 
<title>Facebook Login JavaScript Example</title> 
<meta charset="UTF-8"> 

<script type="text/javascript"> 
$(document).ready(function(){ 
    function std() { 
     FB.api('/me', function(response) { 
      var details = JSON.stringify(response); 
      var name = details.first_name + details.last_name; 
      var email = details.email; 
      var gender = details.gender; 
     }); 

     $.ajax({ 
      type: "POST", 
      url: "std.php", 
      data: "&name="+name+ "&email=" +email+ "&gender=" +gender, 
      success: function(){ 
       alert("Success"); 
      } 
     }); 
    } 
}); 
</script> 
</head> 
<body> 
<div id="fb-root"></div> 
<div id="fb-root"></div> 
<script> 
(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_GB/sdk.js#xfbml=1&version=v2.5&appId=AppID"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'facebook-jssdk')); 
</script> 

<div class="fb-login-button" data-max-rows="3" data-size="large" data-show-faces="false" data-auto-logout-link="true" scope="public_profile,email" onlogin="std();"> 
</div> 
</body> 
</html> 

PHP:

$servername = "localhost"; 
$username = "username"; 
$password = "password"; 

$connection = mysql_connect($servername, $username, $password); 
if (!$connection){ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("database", $connection); 

$name = $_POST['name']; 
$name = mysql_real_escape_string($name); 

$email = $_POST['email']; 
$email = mysql_real_escape_string($email); 

$gender = $_POST['gender']; 
$gender = mysql_real_escape_string($gender); 

$sql = "INSERT INTO newmember VALUES ('', '{$name}', '{$email}' ,'{$gender}', '' ,'')"; 

$db->query($sql); 

mysql_close($connection); 
+0

聲明變量方FB.api功能。 –

+0

響應後發出ajax調用。不在外邊Fb功能 –

回答

1

你的代碼應該是

<html> 
<head> 
    <title>Facebook Login JavaScript Example</title> 
    <meta charset="UTF-8"> 

    <script type="text/javascript"> 
     $(document).ready(function() {    
     }); 
     function std() { 
      FB.api('/me', function (response) { 

       //var details = JSON.stringify(response); 
       var details = response; 
       var name = details.first_name + details.last_name; 
       var email = details.email; 
       var gender = details.gender; 

       $.ajax({ 
        type: "POST", 
        url: "std.php", 
        data: "&name=" + name + "&email=" + email + "&gender=" + gender, 
        success: function() { 
         alert("Success"); 
        } 
       }); 
      }); 

     } 
    </script> 
</head> 
<body> 
    <div id="fb-root"></div> 
    <div id="fb-root"></div> 
    <script> 
     (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_GB/sdk.js#xfbml=1&version=v2.5&appId=AppID"; 
      fjs.parentNode.insertBefore(js, fjs); 
     }(document, 'script', 'facebook-jssdk'));</script> 

    <div class="fb-login-button" data-max-rows="3" data-size="large" data-show-faces="false" data-auto-logout-link="true" scope="public_profile,email" onlogin="std();"> 
    </div> 
</body> 
</html> 
+0

你確定'var details = JSON.stringify(response);'? – Rayon

+0

你正在寫。我錯過了,那是錯誤的。 –

+0

嘗試過@ParthTrivedi,但這並沒有幫助解決問題。 –

0

添加的jQuery在HTML文件的頭部,你的標題以上。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
相關問題