0

我正在尝试创建一个社交约会网站,但我在“添加为好友”按钮中遇到了问题。当您单击它时,我的站点应该将发送者的 uid (from_uid) 和接收者的 uid (to_uid) 发送到数据库。它成功发送了 from_uid,但 to_uid 始终发送 0。

PHP

<div class="member" data-user="<?php echo $member['xmpp_user']; ?>" data-uid="<?php echo $member['uid']; ?>">
  <input type="hidden" id="hiddenuid" value="<?php echo $member['uid']; ?>">
  <img src="https://s3.amazonaws.com/wheewhew/user/<?php echo $member['uid']; ?>/photos/<?php echo $member['profile_pic']; ?>" />
  <div class="member_name"><?php echo $member['firstname']." ".$member['lastname']; ?></div>
  <div id="addbutton"><button type="submit" class="add"> Add as Buddy </button></div>
</div>

Javascript

<script type="text/javascript">
   var BOSH_SERVICE = 'http://wheewhew.com:5280/http-bind';
   var connection   = null;
   var xmpp_user    = "<?php echo $xmpp_user; ?>@wheewhew.com/default";
   var xmpp_pass    = "<?php echo $xmpp_password; ?>";
   var uid          = "<?php echo $uid; ?>";

   $(document).ready(function () {              
     $('#btn-logout').click(logout);
     $('.add').click(addBuddy);
     connectXMPP();
     //updateLastSeen();
   });

</script>

jabber.js

function addBuddy(){
  var xmpp_user = $(this).parent().attr('data-user')+'@wheewhew.com/default';
  var to_uid = $(this).parent().attr('data-uid');

  $.ajax({
    type: "POST",
    url: "./ajax/addBuddy",
    data: "from_uid="+uid+"&to_uid="+to_uid,

    success: function(data) {
      var ret = eval('('+data+')');
      if(ret.status == 'success'){
        connection.send($pres({to:xmpp_user,type:'subscribe'}).tree());
      }
    }

  });
}
4

1 回答 1

0

您的addBuddy功能没有参考$(this)。试试这个:

$('.add').click(function(){
     addBuddy($(this));
});

然后在你的函数中:

function addBuddy($btn){
    var xmpp_user = $btn.parent().attr('data-user')+'@wheewhew.com/default';
    var to_uid = $btn.parent().attr('data-uid');

    // ajaxy stuff
}

那应该有帮助。至少,我们可以在这里继续讨论,而不是在 OP 的评论中。

于 2013-04-24T17:24:46.273 回答