phpjquery-uiautocompleteundefined

Autocomplete is returning undefined


This is my jquery function:

$(function() {
    $("#user").autocomplete({
        source: 'spiderman/null/users.php',
        minLength: 2
    });
});

And when I start typing a minimum 2 letters in my

<input type="text" class="highlight" id="user" name="user"/>

It is showing a item list saying "undefined". Seems like it is a invalid source, but I'm pretty sure it is valid, since I've maked include_once in the same dir, and the source script was loaded.

Here is my PHP code:

if( !$_GET['term'] ) {
     die();
}

$return_arr = array();
$ac_term = "%".$_GET['term']."%";
$query = "SELECT `name` FROM `users` WHERE `name` LIKE :term";
$result = $conn->prepare($query);
$result->bindValue(":term", $ac_term);
$result->execute(); 

/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $row_array['name'] = $row['name'];

    array_push($return_arr, $row_array);
}

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

I've tested the PHP code also, and it works well: as for the term lol it does returned:

[{"name":"LolShit"},{"name":"Lolipop"},{"name":"Lolo"},{"name":"Lolololololololo"},{"name":"Loll"},{"name":"Pro Lol"}]

Solution

  • I've added

    $row_array['value'] = $row['name'];

    And it worked... Dammit. I hope it's ok to answer own question, lol.