简体   繁体   中英

Php Js Ajax I need some assistance

I am trying to create a social dating site and I have my problem in the Add as Buddy Button. When you click it, my site should be sending the uid of the sender (from_uid) and the uid of the receiver (to_uid) to the database. It sends out the from_uid successfully but the to_uid always sends 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());
      }
    }

  });
}

Your addBuddy function doesnt have a reference for what $(this) is. Try this out:

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

And then in your function:

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

    // ajaxy stuff
}

That should help. At the very least, we can continue the discussion here rather than in the comments for the OP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM