jquery - Fill array with json response -


i'm trying populate array in validation code distinct values 1 column, , logic on focus on element send ajax request , populate array json response. i'm still novice in jquery , seems cant right, if 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 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"}] 

just loop array , push

for (var = 0; < data.length; i++) {     cities.push(data[i].city); } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -