0

我正在使用 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>
4

1 回答 1

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);
于 2013-04-04T17:33:56.827 回答