Ajax,如果多于一个@mention [英] Ajax if more then one @mention

查看:51
本文介绍了Ajax,如果多于一个@mention的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery ajax php创建一个Facebook和Twitter风格的提及系统,但是如果我尝试@提及超过一个用户,就会遇到问题.例如,如果我开始输入如下内容:

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. 

显示@stack的结果,但如果我尝试提及这样的另一个用户:

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

Hi @stack how are you. i am @azzo

然后结果一无所获.我想念我的Ajax代码是什么,有人可以帮助我吗?我认为搜索user_name存在正则表达式问题.当我在第一个类似 @stack 的用户名之后写一些用户名时,ajax请求发布此信息:

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:

@ stack你好.我是@a ,然后ajax请求看起来像这样:

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

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

所以我的意思是,显然,ajax会查询以@开头的所有单词.它需要做的是查询数据库中的最后一个@mention.

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

这也是一个用于从数据库搜索用户的php部分:

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

推荐答案

似乎您要发送的数组是AJAX请求中 match 的结果.

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

尽管我无法对其进行测试,但是您可以在正则表达式中使用前瞻性,并使用结果数组中的第一个元素.负向超前(?!.* @ \ w)用于确保我们仅匹配最后一个元素.

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

这篇关于Ajax,如果多于一个@mention的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆