Kailea,
There are problems in your success function, and I suspect problems in your PHP, though I don't understand your objective in using array_slice. Normally, the elements retrieved in the loop are push() into an array.
Here's working jQuery that takes a country name input and displays states in that country. Sorry the format is messy, but its a copy/paste problem. You can make the translation to your needs. It uses POST, which is better and more secure, but you can use GET.
$('#state').autocomplete({
source: function(request, response) {
//Pass country to the php query manager to limit the state selection to just that country
$.ajax({
url: '../phpFunctions/getState.php',
data: {
term: request.term,
country: $('input[name=country]:checked').val() //Pass the selected countryAbbreviation
},
type: 'POST', // a jQuery ajax POST transmits in querystring format (key=value&key1=value1) in utf-8
dataType: 'json', //return data in json format
success: function(data) {
response($.map( data, function(item) {
return {
label: item.stateName,
value: item.name,
abbrev: item.stateAbbrev
};
}));
}
});
},
select: function(event, ui) {
$(this).val(ui.item.value);
$('#hiddenState').val(ui.item.abbrev); //set the state abbreviation
}
Here is a working PHP loop that pumps out JSON
/* Retrieve the query results and store in an array. Specify a MySQL associative array with case sensitive field names.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC))
{
/* In each row, pull stateNameInEnglish and stateProvinceAbbreviation. */
$row_array['stateName'] = $row['stateNameInEnglish'];
$row_array['name'] = $row['stateNameInEnglish'];
$row_array['stateAbbrev'] = $row['stateProvinceAbbreviation'];
array_push($return_arr,$row_array);
}
}
/* Toss back state results as a json encoded array. */
echo json_encode($return_arr);
Lastly, you should copy the JSON response from the server and paste it into jsonlint.com to validate that you get correct JSON format out of your server. Let me know if you need help to do that. Incorrect format is unrecognizable to Autocomplete.