How to populate datas into a Select using jQuery ajax json, php

How to populate datas into a Select using jQuery ajax json, php

Hi !!

      I wrote code to retrive values in select box using json,ajax & PHP.

 

Ajax post to get data from DB .......

$.post("<?=base_url()?>index.php/rubro/list_ajax/", {
 
'categoryID' : idc },
  function(data) {
    var sel = $("#select");
    sel.empty();
    for (var i=0; i<data.length; i++) {
      sel.append('<option value="' + data[i].id + '">' + data[i].desc + '</option>');
    }
  }, "json");

 

Fetching data from db ...

$idcateg = trim($this->input->post('categoryID'));
$result = array();
$id = mysql_real_escape_string($idcateg);
$res = mysql_query("SELECT * FROM subcategories WHERE category = $id");
while ($row = mysql_fetch_array($res)) {
  $result[] = array(
    'id' => $row['subcatid'],
    'desc' => $row['description'],
  );
}
echo json_encode($result);