4

I am trying to make a facebook and twitter style mention system using jquery ajax php but i have a problem if i try to @mention more then one user. For example if i start to type something like the follow:

Hi @stack how are you. 

The results showing @stack but if i try to mention another user like this:

Hi @stack how are you. i am @azzo

Then the results are nothing. What i am missing my ajax code anyone can help me please ? I think there is a regex problem for search user_name. When i write some username after first one like @stack then the ajax request posting this:

f   : smen
menFriend   : @stack
posti   : 102

But if i want to tag my other friend in the same text like this:

Hi @stack how are you. I am @a then ajax request looks like this:

f   : smen
menFriend   : @stack, @a
posti   : 102

So what I'm saying is that apparently, ajax interrogates all the words that begin with @. It needs to do is interrogate the last @mention from database.

   var timer = null;
   var tagstart = /@/gi;
   var tagword = /@(\w+)/gi;
   $("body").delegate(".addComment", "keyup", function(e) {
    var value = e.target.value;
    var ID = e.target.id;
    clearTimeout(timer);
    timer = setTimeout(function() {
      var contents = value; 
      var goWord = contents.match(tagstart);
      var goname = contents.match(tagword); 
      var type = 'smen'; 
      var data = 'f=' +type+ '&menFriend=' +goname +'&posti='+ID;
      if (goWord.length > 0) { 
             if (goname.length > 0) { 
                $.ajax({
                type: "POST",
                url: requestUrl + "searchuser", 
                data: data,
                cache: false,
                beforeSend: function() {
                    // Do Something
                },
                success: function(response) { 
                    if(response){
                        $(".menlist"+ID).show().html(response);
                    }else{
                        $(".menlist"+ID).hide().empty();
                    }
                }
              });
         } 
      }  
    }, 500); 
  });

Also here is a php section for searching user from database:

   $searchmUser = mysqli_real_escape_string($this->db,$searchmUser);
   $searchmUser=str_replace("@","",$searchmUser);
   $searchmUser=str_replace(" ","%",$searchmUser);
   $sql_res=mysqli_query($this->db,"SELECT 
   user_name, user_id 
   FROM users WHERE 
   (user_name like '%$searchmUser%' 
   or user_fullname like '%$searchmUser%') ORDER BY user_id LIMIT 5") or die(mysqli_error($this->db));  
   while($row=mysqli_fetch_array($sql_res,MYSQLI_ASSOC)) {
           // Store the result into array
           $data[]=$row;
        }
        if(!empty($data)) {
           // Store the result into array
           return $data;
        }
AlwaysStudent
  • 1,354
  • 18
  • 49
  • Possible duplicate of [jQuery send string as POST parameters](https://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameters) – guychouk Dec 23 '18 at 20:11
  • @spongeworthy What is that ? I am asking about mention system there is not have any answer about my question. – AlwaysStudent Dec 23 '18 at 20:40
  • 1
    So you only want to send `menFriend: @azzo` for input text: `Hi @stack how are you. I am @azzo` ? – anubhava Dec 24 '18 at 11:06
  • 1
    @anubhava yes . And also if i want to send another after `@azzo` like `@anubhava` then it should send `@anubhava`. – AlwaysStudent Dec 24 '18 at 11:12
  • @anubhava azzo is username from users table. I need azzo not `@azzo` but you can see i will remove @ using str_replace in a tex – AlwaysStudent Dec 24 '18 at 11:36

1 Answers1

1

Looks like you're sending an array which is result of match you in AJAX request.

Though I cannot test it but you can use a lookahead in your regex and use 1st element from resulting array. Negative lookahead (?!.*@\w) is used to make sure we match last element only.

   var timer = null;
   var tagword = /@(\w+)(?!.*@\w)/;

   $("body").delegate(".addComment", "keyup", function(e) {
      var value = e.target.value;
      var ID = e.target.id;
      clearTimeout(timer);
      timer = setTimeout(function() {
         var contents = value;
         var type = 'smen'; 
         var goname = contents.match(tagword); 

         if (goname != undefined) {
            var data = 'f=' +type+ '&menFriend=' +goname[1] +'&posti='+ID;
            $.ajax({
               type: "POST",
               url: requestUrl + "searchuser",
               data: data,
               cache: false,
               beforeSend: function() {
                  // Do Something
               },
               success: function(response) {
                  if(response){
                     $(".menlist"+ID).show().html(response);
                  } else {
                     $(".menlist"+ID).hide().empty();
                  }
               }
            });
         }
      }, 500);
   });
anubhava
  • 761,203
  • 64
  • 569
  • 643