jQuery populate items into a Select using jQuery ajax json, php

后端 未结 4 448
感动是毒
感动是毒 2020-12-14 05:05

I have a select field. I must fill with options taken from a mysql table.
Here is some little php code I have done using codeigniter framework

$idcateg =         


        
4条回答
  •  [愿得一人]
    2020-12-14 05:30

    It's not much different.

    $idcateg = trim($this->input->post('idcategory'));
    $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);
    

    with:

    $.post("index.php/rubro/list_ajax/", { 
      'idcategory' : idc },
      function(data) {
        var sel = $("#select");
        sel.empty();
        for (var i=0; i' + data[i].desc + '');
        }
      }, "json");
    

提交回复
热议问题