I did a lot of searching and I got a lot of results similar to my case, listed below:
- Jquery UI autocomplete ajax is not populating dropdown box
- jQuery UI GET autocomplete source with ajax request
- JQuery UI autocomplete with json and ajax
Data source is from text file which format like this:
postcode,suburb,state,lat,lon
200,AUSTRALIAN NATIONAL UNIVERSITY,ACT,-35.277272,149.117136
221,BARTON,ACT,-35.201372,149.095065
800,DARWIN,NT,-12.801028,130.955789
801,DARWIN,NT,-12.801028,130.955789
804,PARAP,NT,-12.432181,130.84331
810,ALAWA,NT,-12.378451,130.877014
810,BRINKIN,NT,-12.367769,130.869808
810,CASUARINA,NT,-12.376597,130.850489
810,JINGILI,NT,-12.385761,130.873726
810,LEE POINT,NT,-12.360865,130.891349
The data I'm trying to include as source of auto-complete field is the first and second(postcode and suburb) fields of each row. But the postcode and suburb filter with state. For example ACT is the active state all the postcode and suburb is the source of auto-complete. And I used array_unique() to remove the repeated data.
//take area from state
function take_area(){
global $wpdb;
$uploads = wp_upload_dir();
$upload = $uploads[baseurl];
$file = $upload.'/csv/suburb_and_area.txt';
$f = fopen($file, 'r');
$state = $_POST['state'];
$counter = 0;
while($line = fgets($f, 4096)){
$details = explode(',', $line);
$counter++;
if(trim($details[2]) == $state){
$state_arr[$counter] = $details[1];
}
}
$option = '';
if($state!=''){
$c=0;
$area_of_state = array_unique($state_arr);
foreach($area_of_state as $area){
if($c>0){$option .= ', ';}
$option .= '"'.$area.'"';
$c++;
}
}
echo $option;
}
and the JavaScript:
$("#state").change(function(){
var state = $('#state :selected').attr('data-value');
if(!state){
state = $('#state').val();
}
$.ajax({
url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
type:'POST',
data:'action=take_area_from_state&state=' + state,
success:function(results){
if(results!=0){
$("#area").removeAttr("disabled");
$("#area").empty();
var source = [results];
$("#area").autocomplete({source: source});
}else{
$("#area").attr("disabled", "disabled");
}
}
});
});
And the Response
"AUSTRALIAN NATIONAL UNIVERSITY", "BARTON", "HMAS CRESWELL", "JERVIS BAY", "CANBERRA", "DEAKIN", "DEAKIN WEST", "DUNTROON", "HARMAN", "HMAS HARMAN", "PARKES", "PARLIAMENT HOUSE", "RUSSELL", "YARRALUMLA", "ACTON", "BLACK MOUNTAIN", "AINSLIE", "DICKSON", "DOWNER", "HACKETT", "LYNEHAM", "O'CONNOR", "WATSON", "FORREST", "GRIFFITH", "MANUKA", "RED HILL", "CAUSEWAY", "KINGSTON", "NARRABUNDAH", "CURTIN", "GARRAN", "HUGHES", "CHIFLEY", "LYONS", "O'MALLEY", "PHILLIP", "SWINGER HILL", "WODEN", "FARRER", "ISAACS", "MAWSON", "PEARCE", "TORRENS", "CIVIC SQUARE", "CANBERRA INTERNATIONAL AIRPORT", "FYSHWICK", "MAJURA", "PIALLIGO", "SYMONSTON", "CHAPMAN", "DUFFY", "FISHER", "HOLDER", "MOUNT STROMLO", "PIERCES CREEK", "RIVETT", "STIRLING", "URIARRA", "URIARRA FOREST", "WARAMANGA", "WESTON", "WESTON CREEK", "BRADDON", "CAMPBELL", "REID", "TURNER", "ARANDA", "COOK", "HAWKER", "JAMISON CENTRE", "MACQUARIE", "PAGE", "SCULLIN", "WEETANGERA", "CHARNWOOD", "DUNLOP", "FLOREY", "FLYNN", "FRASER", "HIGGINS", "HOLT", "KIPPAX", "LATHAM", "MACGREGOR", "MELBA", "SPENCE", "BELCONNEN", "BRUCE", "EVATT", "GIRALANG", "KALEEN", "LAWSON", "MCKELLAR", "UNIVERSITY OF CANBERRA", "HALL", "HUME", "KOWEN FOREST", "OAKS ESTATE", "THARWA", "TOP NAAS", "GREENWAY", "TUGGERANONG", "KAMBAH", "ERINDALE CENTRE", "OXLEY", "WANNIASSA", "FADDEN", "GOWRIE", "MACARTHUR", "MONASH", "BONYTHON", "CALWELL", "CHISHOLM", "GILMORE", "ISABELLA PLAINS", "RICHARDSON", "THEODORE", "BANKS", "CONDER", "GORDON", "CRACE", "MITCHELL", "GUNGAHLIN", "FRANKLIN", "GINNINDERRA VILLAGE", "NGUNNAWAL", "NICHOLLS", "PALMERSTON", "AMAROO", "BONNER", "FORDE", "HARRISON"
The data is displayed like this:

I know I've done wrong to my code but I can't figure it out.
$("#area").autocomplete({ source: results })?$("#area").autocomplete({ source: results })that is not display anything. I fail to clear my browser cache.