我正在使用facebook javascript sdk来获取登录用户的公共数据。我想要做的是将值绑定到表单中的隐藏字段,并在提交时,获取将插入MySQL数据库的php文件中的值。但是当我提交表格时,没有价值观。这是为什么?如果我需要提供更多信息,请告诉我。当我登录时,我可以在网络应用程序上看到我的Facebook名称和个人资料照片。
//Hidden fields within form
<input type="hidden" name="hdnFacebookId" id="hdnFacebookId" value="">
<input type="hidden" name="hdnUsername" id="hdnUsername" value="">
<input type="hidden" name="hdnFirstName" id="hdnFirstName" value="">
<input type="hidden" name="hdnLastName" id="hdnLastName" value="">
<input type="hidden" name="hdnFacebookImg" id="hdnFacebookImg" value="">
<input type="hidden" name="hdnFacebookUrl" id="hdnFacebookUrl" value="">
<br/>
<input type="submit" id="submit" onclick="setValues()" data-theme="b" value="Submit" class="ui-btn-hidden" aria-disabled="false">
<script>
function updateUserInfo(response) {
FB.api('/me', function(response) {
document.getElementById('myFacebookImg').src = "https://graph.facebook.com/" + response.id + "/picture";
document.getElementById('myFacebookName').innerHTML = response.name;
function setValues(){
document.postAJourneyForm.hdnFacebookId.value = response.id;
document.postAJourneyForm.hdnUsername.value = response.username;
document.postAJourneyForm.hdnFirstName.value = response.first_name;
document.postAJourneyForm.hdnLastName.value = response.last_name;
document.postAJourneyForm.hdnFacebookImg.value = "https://graph.facebook.com/" + response.id + "/picture";
document.postAJourneyForm.hdnFacebookUrl.value = "http://graph.facebook.com/" + response.username;
//document.forms["postAJourneyForm"].submit();
}
});
}
//php file that gets the data to post to database
//I check in the code if it is empty before I can execute the query
//but it is always empty
//get the facebook data
$hdnFacebookId = $_POST['hdnFacebookId'];
$hdnUsername = $_POST['hdnUsername'];
$hdnFirstName = $_POST['hdnFirstName'];
$hdnLastName = $_POST['hdnLastName'];
$hdnFacebookImg = $_POST['hdnFacebookImg'];
$hdnFacebookUrl = $_POST['hdnFacebookUrl'];
//Updated with JSON
<script>
function updateUserInfo(response) {
FB.api('/me', function(response) {
document.getElementById('myFacebookImg').src = "https://graph.facebook.com/" + response.id + "/picture";
document.getElementById('myFacebookName').innerHTML = response.name;
document.getElementById('facebookUrl').href = "https://www.facebook.com/" + response.username;
var JSONObject = {
"facebookId":response.id,
"facebookName":response.username,
"facebookFirstName":response.first_name,
"facebookLastName":response.last_name
};
document.getElementById('hdnFacebookId').innerHTML = JSONObject.facebookId;
document.getElementById('hdnUsername').innerHTML = JSONObject.facebookName;
document.getElementById('hdnFirstName').innerHTML = JSONObject.facebookFirstName;
document.getElementById('hdnLastName').innerHTML = JSONObject.facebookLastName;
document.getElementById('hdnFacebookImg').innerHTML = JSONObject.facebookImage;
document.getElementById('hdnFacebookUrl').innerHTML = JSONObject.facebookUrl;
});
}
</script>
答案 0 :(得分:1)
设置值。
$('#hdnFacebookId').val(response.id);
$('#hdnUsername').val(response.username);
$('#hdnFirstName').val(response.first_name);
$('#hdnLastName').val(response.last_name);
$('#hdnFacebookImg').val('https://graph.facebook.com/' + response.id + '/picture');
$('#hdnFacebookUrl').val('http://graph.facebook.com/' + response.username);