I'm trying to populate array in my validation code with distinct values from one column, and logic was that on focus on certain element I would send ajax request and populate array with JSON response. But I'm still a novice in JQuery and it seems I cant get it right, so if anyone can help.
$('input#search').on("focus", function(){
$.ajax({
type:"get",
dataType: "json",
url:"ajax_php/get_distinct_cities.php",
success:function(data){
$.each( data.city, function( i, itemData ) {
cities[i] = itemData;
});
}
});
});
PHP:
<?php
mysql_connect("localhost","root","");
mysql_select_db("pickante");
$sql = "SELECT DISTINCT city FROM uc_items";
$result = mysql_query($sql);
$cities = array();
while($row = mysql_fetch_assoc($result))
{
$cities[] = $row;
}
echo json_encode($cities);
?>
JSON Print:
[{"city":"Belgrade"},{"city":"Novi Sad"}]
datais an array, it doesn't have a.cityproperty.$.each(data, function(i, itemData){ cities[i] = itemData.city; });.