Tutorial :Search for multiple words with jQuery Autocomplete



Question:

I am really struggling with jQuery's autocomplete plugin. I have a large field in my database with multiple words in it, seperated by spaces:

forename surname address postcode

I can get the autocomplete to search for a single word. E.g. if I type "forename" I get an output of:

forename surname address postcode.

However, if I type "forename surname" I get nothing.

Likewise, the postcode is split into two words e.g. AB1 2CD, AB2 2CD. If I type "AB1" I can see "AB1 2CD". However if I type "AB1 2CD" I see both results. In Firebug it appears that a new get request is triggered after the space is entered.

I have the multiple option selected, with a seperator of " ". This is the code of how the autocomplete is being called:

$().ready(function() {        $("#autocomplete input#autotext").autocomplete("clientsearchtest.php", {            width: 400,             multiple: true,           minChars: 2,          cacheLength: 1,          multipleSeparator: " "      });    });     

More than just needing "AB1 2CD" to work, I want the autocomplete to also show the result "AB1 2CD" when "2cd ab1", "2cd", "ab1" "ab cd" or "ab1 2c" is entered. This is driving me crazy, any help greatly appreciated!

I think what I need to do is somehow send each spaced string as a different variable to my PHP script. so currently the entire string is sent as $q. I really need to send $q1, $q2, each of which defined by the presence of a space character. That way my SQL select statement can have a where clientsearch LIKE '%$q%' AND '%$q2'.


Solution:1

As Raja commented, you should build your response controller to handle this logic rather than modifying the jQuery, I have used the same solution and it works great. You might want to do some performance testing and make sure that the LIKE query isn't too heavy though.


Solution:2

I don't know if you have checked this demo page for autocomplete. There you can see different types of your requirements are fulfilled. Just see if you get any help from there.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »