|
|
|
Facebook javascript SDK login อยากโอนจากค่าในjavascript มา PHP ทำอย่างไรดีครับ ajax |
|
|
|
|
|
|
|
ตอนนี้บริษัทผมใช้ PHP version 5.2 เองครับ เลยยังไม่อยากใช้facebook API ของPHP เพราะAPI facebookของวิธีPHP ตัวใหม่ๆ มันrequire PHP ver 5.4 ขึ้นไป
ก็เลยสนใจใช้ javascript แทนครับ
ปัญหาคือ อยากจะเก็บค่าของข้อมูลผู้ใช้ที่login facebookเข้ามาในเว็บ ลงในdatabase mySQL ด้วยครับ เลยอยากสอบถามว่าจะโอนไปหาPHPอย่างไรดี ทีนี้ค่าที่ผมอยากโอนคือ ให้ลองดูในfunction getInfo() ดูครับ ตัวresponse.emailนั่นแหละครับ ซึ่งผมจะส่งค่าไปหาไฟล์ที่ชื่อ "new 1.php"
รบกวนด้วยครับ ทำวิธีผมแล้วมันดันขึ้นerror Undefined index: email in C:\xampp\htdocs\new 1.php on line 3
/// copy code ของผมไปเลยครับ แล้วไปrunชิวๆ เพราะวิธีjs มันไม่ต้องลงอะไรเพิ่มอยู่แล้วครับ
Code (JavaScript)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '201637766943985', /// copyของผมไปเลยครับ แล้วไปrunชิวๆ เพราะวิธีjs มันไม่ต้องลงอะไรเพิ่มอยู่แล้ว
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; /////////ตัวแปรที่ต้องการส่งไปหาPHP///////////
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();
});
}
</script>
<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>
</body>
</html>
ในหน้า new 1.php
Code (PHP)
<?php
if($_SERVER['REQUEST_METHOD'] == 'GET'){
$email = $_GET['email'];
echo $email;
}
?>
Tag : PHP, JavaScript
|
ประวัติการแก้ไข 2016-11-02 19:16:32 2016-11-02 19:18:17 2016-11-02 20:39:51
|
|
|
|
|
Date :
2016-11-02 19:15:57 |
By :
BHunter |
View :
1501 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (JavaScript)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '201637766943985', /// copyของผมไปเลยครับ แล้วไปrunชิวๆ เพราะวิธีjs มันไม่ต้องลงอะไรเพิ่มอยู่แล้ว
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; /////////ตัวแปรที่ต้องการส่งไปหาPHP///////////
var http = new XMLHttpRequest(); ///////////AJAX///////
var url = "new 1.php";
var data = "email=" + email;
http.open("GET", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //<-------- Add this
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();
});
}
</script>
<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>
</body>
</html>
|
ประวัติการแก้ไข 2017-10-01 11:22:46
|
|
|
|
Date :
2017-10-01 11:22:07 |
By :
TRIBIIZ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แนะนำให้ส่งแบบ POST
http.open("POST", 'new1.php', true);
http.send('email=' + encodeURIComponent(email));
เวลารับ
<?php // new1.php
print_r($_POST);
ชื่อไฟล์ไม่ควรมีช่องว่าง
|
ประวัติการแก้ไข 2017-10-02 19:35:26 2017-10-02 19:39:17
|
|
|
|
Date :
2017-10-02 19:34:59 |
By :
goragod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|