1

I am using jQuery autocomplete, and want to have a separate PHP file to house the data, instead of creating a variable (i.e. var availableTags = ["Red", "Orange", "Yellow", "Green, "Blue"]).

However, when doing this, the autocomplete will continually display all of the available options, and not filter out what the user types in.

Any ideas?

Javascript:

$(document).ready(function() {

    $( ".autocomplete" ).autocomplete({
            source: "../../db/autocomplete_list.php"
    });

});

autocomplete_list.php

require_once('../includes/connect.php');
$dbh = connect(DB_HOST,DB_USER,DB_PASSWORD,CM_DATABASE);

$colors = '[';

$sth = $dbh->query ("SELECT * FROM constants 
                     WHERE category='Colors' 
                     ORDER BY display");

while ($row = $sth->fetch ()) { 

    $colors.= '"'.$row[value].'", '; 

}

$colors = rtrim($colors, ', ');

$colors .= ']';

print $colors;

execution of autocomplete_list.php

["Red", "Orange", "Yellow", "Green, "Blue"]

EDIT:

Have updated my autocomplete_list.php file to the following. However, now the autocomplete isn't displaying my values. If I check the response in FireBug, it is showing the results correctly, it just appears that they aren't being passed back to the autocomplete.

require_once('../includes/connect.php');
$dbh = connect(DB_HOST,DB_USER,DB_PASSWORD,CM_DATABASE);

if(isset($_GET['term'])) {
    $mysearchString = $_GET['term'];
}

$sth = $dbh->query("SELECT category, value, display FROM constants 
                    WHERE category = 'COLORS' AND value LIKE '$mysearchString%'
                    ORDER BY display");

while ($row = $sth->fetch ()) { 
    print $row['display']."\n";         
}
5
  • Are you seeing any errors on the page? Commented Nov 20, 2011 at 23:06
  • No errors. Have FireBug open, and nothing. "GET dev.sample.com/db/autocomplete_list.php?term=l" with response of "Yellow", "Blue". Commented Nov 20, 2011 at 23:44
  • It should look like an array in the console. Is that the case? Commented Nov 21, 2011 at 0:44
  • 2
    json_encode with php and decode with jquery Commented Nov 21, 2011 at 1:20
  • I figured it out, and posted an answer below! Thanks! Commented Nov 21, 2011 at 1:36

3 Answers 3

1

the best way is to reduce the possible options on server side:

$sth = $dbh->query ("SELECT * FROM constants 
                     WHERE category='Colors' and value like '.".$_GET['term']."%'
                     ORDER BY display");

(with $_GET['term'] replaced by a string checked against sql injections..)

see: http://docs.jquery.com/UI/Autocomplete

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

1 Comment

Made the changes, but now the autocomplete is not displaying on my live form. When I check the console on Firebug, it is calling everything correctly. Any ideas?
1

Found a solution here:

http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/

require_once('../includes/connect.php');
$dbh = connect(DB_HOST,DB_USER,DB_PASSWORD,CM_DATABASE);

$return_arr = array();

if(isset($_GET['term'])) {
    $mysearchString = $_GET['term'];
}

$sth = $dbh->query("SELECT category, value, display FROM constants 
                    WHERE category = 'COLORS' AND value LIKE '$mysearchString%'
                    ORDER BY display");

while ($row = $sth->fetch PDO::FETCH_ASSOC()) { 

     $row_array = $row['display'];

     array_push($return_arr,$row_array);

}

echo json_encode($return_arr);

Comments

0

source propery expect an array of values, not a string. Try to echo the content of "autocomplete_list.php" directly onto the script, something like this:

source: <?php include "../../db/autocomplete_list.php"; ?>

That will should work.

1 Comment

Thought about that, but you can't do a PHP include in Javascript.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.