Php Js Ajax我需要一些帮助

时间:2013-04-24 16:33:21

标签: php javascript jquery ajax

我正在尝试创建一个社交约会网站,我在Add as Buddy Button中遇到了我的问题。当您单击它时,我的站点应该将发送方的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());
      }
    }

  });
}

1 个答案:

答案 0 :(得分: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的评论中。