我試圖在用戶登錄後在數據庫中保存用戶詳細信息。我正在使用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);
聲明變量方FB.api功能。 –
響應後發出ajax調用。不在外邊Fb功能 –