Seach

how to populate a dropdown list in codeigniter using json,jquery,ajax

view

<head>
    <script src="<?php echo base_url().'js/jquery.js';?>"></script>
</head>
<form method="post" >
    <select id="select">
        
    </select>
</form>

<script>
   $(document).ready(function(e){
       $.ajax({
           dataType: 'json',
           url: "json_",
           type: 'POST',
           processData: false,
           cache: false,error: function (jqXHR, textStatus, errorThrown) {
                        alert('error occured');
                    },
           success: function (data, textStatus, jqXHR)
           {
    
//           $.each(data,function(key, value){
//             $('<option />', {value: value
//                         .actor_id, text: value.first_name}).appendTo("#select");
//        });
    
$.each(data,function(key,value)
{
    $('</option>',{value:value.actor_id,text:value.first_name}).appendTo("#select");
});}            
       });
   });
</script>

controller
public function json_()
    {
        $this->load->model('json_model');
        $tmp=$this->json_model->fetch();
        echo json_encode($tmp);
        
    }

model

public function fetch()
    {
        $this->db->select('actor_id,first_name');
        $this->db->from('actor');
        $data=$this->db->get();
        return $data->result_array();
    }