0

Hi everyone i have one problem with my ajax tag search system. Following code working fine just one searching tag. But if i search multiple tag then it is not working. What i need to do here? What i am missing anyone can help me here ?

For example: When i write #innovation the the ajax will showing #innovation but when i search multiple tag like #innovation asking #stackoverflow then it is not searching #stackoverflow

 $(document).ready(function() {

  var start=/#/ig;
  var word=/#(\w+)/ig;

  $("#contentbox").live("keyup",function()  {
  var content=$(this).text();
  var go= content.match(start);
  var name= content.match(word);
  var dataString = 'searchword='+ name;

  if(go!==null && go.length!==0) {
     $("#msgbox").slideDown('show');
     $("#display").slideUp('show');
     $("#msgbox").html("Type the name of someone or something...");
     if(name!==null && name.length!==0) {
        $.ajax({
               type: "POST",
               url: "boxsearch.php",
               data: dataString,
               cache: false,
               success: function(html) {
                        $("#msgbox").hide();
                        $("#display").html(html).show();
               }
       });

     }
  }
  return false;
});
});

I think the problem is the following line :

var start=/#/ig;
var word=/#(\w+)/ig;
5
  • What is #innovation and what is #stackoverflow? Also you shouldn't use live. It's deprecated. Commented Nov 19, 2015 at 21:44
  • @KФ that is just hashtag example dear. Commented Nov 19, 2015 at 21:44
  • @KФ I know live is deprecated that is just an example. I need multiple hashtag search in my test ajax code. Do you know what i am missing ? Commented Nov 19, 2015 at 21:46
  • Does your PHP code get the query string correctly? Commented Nov 19, 2015 at 21:54
  • @MatthewHerbst yes dear php code working correctly. Commented Nov 19, 2015 at 21:56

2 Answers 2

1

There's a million different ways to crack this nut. One of the methods I used in the past was to send the whole input as a single attribute on the service call, and then pass it into db call (via PHP) using the REGEX feature of MySQL. Using that feature, it searched for any combination of value 1, or value 2 or value 1 and value 2, etc.

But let's attack it from the front end as you're doing. You have an input coming in as a combination of "#word1 #word2". So rather than doing regex summersaults to find the beginning and the end, how about simplifying things and doing a string.split() on the "#" character. You can take the resulting array of values, loop to create your service call string (don't forget to trim blank spaces) and send the call off.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is definetely coming from your regex. The second you write a second word in your textarea (I'm assuming you're using a textarea), the if condition fails.

I'm not regex expert, however I have a js solution:

var keywords = $(this).val().split(' ').trim();

Get all the keywords this way, then loop through each one and check if it's a correct tag.

var obj = [];
keywords.forEach(function (content) {
    var go = content.match(start);
    var name = content.match(word);
    //check to see if the searchword is valid. 
    //If it is we add it to the list and continue the loop. 
    //If not we ignore it and continue the loop.
    //If you want to stop the loop when one searchword is wrong add "return false;"
    if(go !== null && go.length > 0 && name !== null && name.length > 0 ){
       obj.push(name);
    }
 });

 if(obj.length > 0){
     var dataString = JSON.stringify(obj);
     //do ajax call here
 }

I'm also assuming you're sending all your searchwords at once to your php file.

EDIT: Pushing all valid keywords into an array obj that will be later parsed as a JSON string.

2 Comments

So, two challenges. Trim removes whitespace around a string. However, if you trim before you split, you're going to ignore whitespaces between words. Second challenge: by splitting on spaces, you could potentially end up with incorrect values if someone chose to enter a hashtag with a space. Safer to split after the hashtag.
@bpeterson76 I figured that it would never make it through the if condition even if it was an empty string, you are right. It's better after the split. :) thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.