Björn C
Björn C

Reputation: 4008

Filter with Jquery Autocomplete, Remote Source

So, i have implemented "Jquery Autocomplete" to my input field.

HTML

<input type="text" id="objekt_nr">

JS

$(function() {
    $( "#objekt_nr" ).autocomplete({
        source: 'file.php',
        minLength: 2,
    });
});

After some reading (no info in the docs) on google, i understand that you need to have the filter in "Server Side" php script. And i use some filtering there.

PHP

$term = trim(strip_tags($_GET['term']));
$query = "SELECT DISTINCT column1 as value FROM table WHERE column1 LIKE '".$term."%'";

My problem is that the "catch" is only filtering the very first or maby second charater.
And as i type, the miss fitting characters won´t filter out.

Example: I type in 2133.. no i can also see: 2135..
How can i solve this?
Do i need to write my own on keyup, function?
Because i cannot find anything about this in the docs.

Upvotes: 0

Views: 77

Answers (1)

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

If I understand correctly, you want it to filter based on partial matches ( eg when you type 2133, to show results for 2, 21, 213, and 2133 ).

Assuming the above is true, you don't need to do anything in jQuery - entirely in PHP. So, modifying your code above :

$term = trim(strip_tags($_GET['term']));
for($i=1; $i<=strlen($term); $i++) {
  $test_term = substr($term, 0, $i);
  $query = "SELECT DISTINCT column1 as value FROM table 
             WHERE column1 LIKE '".$test_term."%'";
}

Upvotes: 1

Related Questions