使用codeigniter使用ajax / jquery将项添加到下拉列表中

This may be the duplicate question but I would like to mention that other answers have not solved my problem.

As the question says, I want to populate the dropdown using ajax/jquery. I am able to send and recieve data in the JSON format successfully but it does not append with the dropdown. Following is my code:

View

  <select name="guard_id" id="guard_id" style="width:100%;">
    <option>select guard</option>
       <optgroup label="Previously assigned guard" id="trained_guards">
       </optgroup>
  </select>

Jquery/Ajax call

function checkTrainedGuards(sid) {

        $.ajax({
            dataType: 'JSON',
            type: 'POST',
            url: '<?php print site_url('roster/check_trained_guards'); ?>',
            data: { sid: sid},
            beforeSend:function() {
                // this is where we append a loading image
                $('#sid').addClass('loader-roster');
            },
            success:function(data) {
             var appenddata;
             $.each(data, function (key, value) {

             appenddata += "<option value = '" + value.gid + " '>" + value.gname + " </option>";                        
             });
            $('#trained_guards').html(appenddata);

            },
            error:function(){
                // failed request; give feedback to user
            }
        });
    }

Controller

  public function check_trained_guards(){

        $this->load->model("guard");
        $sid=$this->input->post("sid");

        $trainedGuards=$this->guard->getGuardAlreadyWorkedOnSite($sid);

        foreach ($trainedGuards as $guard) {

        print json_encode(array(
            'gid'  => $guard->gid,
            'gname' => $guard->guard_name
        ));
        }
      }
}

When I test it using firebug, I an see the data is coming correct and it is in JSON format as shown below:

[{"gid":"293","gname":"Abc"},{"gid":"96","gname":"guard2"},{"gid":"101","gname":"guard3"},{"gid":"91","gname":"guard4"}]

However, I am just wondering why it is not appending the result into dropdown box?

I, Also tested this by alert(value.gid) in the jquery each loop it says undefined.

Any help will be highly appreciated.

You JSON seems to be in wrong format . Your php part should look like follows :

$arr = array();
foreach ($trainedGuards as $guard) {
$arr[] = $guard;

}

print json_encode($arr);

Now this code will return a valid JSON as Follows. JOSN will the array of objects shown below :

[{"gid":"2","gname":"Gurd1"},{"gid":"3","gname":"Guard2"}]

In your ajax response write following code

 var data = [{"gid":"2","guard_name":"Gurd1"},{"gid":"3","guard_name":"Guard2"}]; 
//This is the response format  You can also use JSON.parse if its in the string format



    var appenddata = "";
    $.each(data, function (key, value) {

    appenddata += "<option value = '" + value.gid + " '>" + value.gname + " </option>";                        
    });
$('#trained_guards').html(appenddata);