I'm using jQuery UI's autocomplete to try to propagate the correct input for a form, with over 1000 vaules.
The autocomplete works fine, but how do I restrict the value of the field to just those values?
Autocomplete code in the form:
$('#animal').autocomplete({
source: "search_species.php",
minLength: 3,
select: function(event, ui) {
$('#animal').val(ui.item.postcodes);
$('#code').val(ui.item.code);
$('#family').val(ui.item.family);
}
});
Code in autocomplete source:
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$return_arr = array();
if ($conn){
$fetch = mysql_query(
"SELECT * FROM animals where animals like '%" . mysql_real_escape_string($_GET['term']) . "%'"
);
/* Retrieve and store in array the results of the query.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['value'] = $row['animals'];
$row_array['family'] = $row['family'];
$row_array['code'] = $row['code'];
array_push($return_arr,$row_array);
}
}
mysql_close($conn);
echo json_encode($return_arr);
As I said, the autocomplete works. I just need to know how to limit the values in the text field to those in the search so that people can't type in their own values.
Thanks in advance.