I'm making an AJAX call with Jquery-autocomplete on a wordpress website (A list of airports as label and the Airport ID as value).
What is returned is an array with both label and ID, but the input is not showing anything as it was when I only returned an single array of names.
The data source :
function ajax_listings() {
global $wpdb;
//get names of all airports
$name = '%'.$wpdb->esc_like(stripslashes($_POST['name'])).'%';
$sql = "select airportid, completename
from _airports
where completename like %s";
$sql = $wpdb->prepare($sql, $name);
$results = $wpdb->get_results($sql);
$titles = array();
foreach( $results as $r ) {
$titles[] = array(
"label" => $r->completename,
"value" => $r->airportid
);
}
echo json_encode($titles);
die();
}
My AJAX call :
$('#start').autoComplete({
source: function(name, response) {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'wp-admin/admin-ajax.php',
data: 'action=get_listing_names&name='+name,
dataType: 'json',
success: function( data ) {
response( $.map( data.d, function( item ) {
return {
label: item.label,
value: item.value
}
}));
}
});
},
select: function (event, ui) {
$('#start').val(ui.item.label); // display the selected text
$('#idstart').val(ui.item.value); // save selected id to hidden input
return false;
},
focus: function (event, ui) {
event.preventDefault();
$(this).val(ui.item.label);
}
});
I want the text input to only show the label of the items as suggestions, but I also need the airport ID to store in a hidden input and use it later.
An example of array that is returned when I type "MON" :
[101] {label: "PLP, Captain Ramon Xatruch Airport, La Palma, Panama", value: "5841"}
[102] {label: "LTI, Altai Airport, Altai, Mongolia", value: "6370"}
...
$.mapcall doesn't look like it changes anything. It returns exactly what you passed into it..map(), the data should already be in the correct Array of Objects format that Autocomplete is looking for. Can you proivide an example of the data that is returned?