Ajax if more then one @mention

Viewed 408

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;
        }
1 Answers

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);
   });
Related