Jquery autocomplete not working with json data

My PHP code is returning JSON data for jQuery autocomplete, but autocomplete doesn't work

Jquery autocomplete

$("input#txtaddkey").autocomplete({ source: "keyword.php", minLength: 2 }); 

Php code

 $fetch = mysql_query("SELECT * FROM o_keyword where keyword like '%" . $query . "%'"); while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) { $row_array['id'] = $row['id']; $row_array['keyword'] = $row['keyword']; array_push($return_arr,$row_array); } echo json_encode($return_arr); 

JSON data output

 [{"id":"2","keyword":"Games"},{"id":"3","keyword":"Goa"}] 

And when typing "Ga", I get an empty li tag at the front end.

+7
json jquery php jquery-autocomplete
source share
2 answers

From:

your JSON should contain label or value (or both). Change keyword to value and it should work fine.

+28
source share

Your code should be slightly modified.

  while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) { $row_array['value'] = $row['id']; $row_array['label'] = $row['keyword']; array_push($return_arr,$row_array); } 

echo json_encode ($ return_arr);

Now your json format will be

 [{"value":"2","label":"Games"},{"value":"3","label":"Goa"}] 
+1
source share

All Articles