Mir Abzal Ali
Mir Abzal Ali

Reputation: 579

codigniter: how to pass array from controller to view with jquery

already my controller index method load with a parameter where two dropdown field, when I select the first one then I get data for the second one with jquery ajax and then when I select second one then I need to pass another array from a list_ByBatch method.

My controller is:

public function index() {
    $data['round'] = $this -> AttendanceModel -> get_round();

    $this->load->view('common/header'); 
    $this->load->view('common/menu');
    $this->load->view('attendance/attendance', $data);
    $this->load->view('common/footer');
}

public function list_ByBatch(){
    $batch=$this->input->post('batchid',TRUE);      
    $list['trainee']=$this->AttendanceModel->get_traineeList($batch);
    if(!empty($list['trainee'])){
        return $list['trainee'];
    }
}

Script:

   <script type="text/javascript">
    $(document).ready(function() { 
        $("#round").change(function(){
            /*dropdown post */
            $.ajax({
            url:"<?php echo base_url(); ?>index.php/attendance/batch_ByRound",    
            data: {round: $(this).val()},
            type: "POST",
            success: function(data){ 
                $("#batch-list").html(data);
            }
            });
        });

        $("#batch-list").change(function(){
            /*dropdown post */
            $.ajax({
            url:"<?php echo base_url(); ?>index.php/attendance/list_ByBatch",    
            data: {batchid: $(this).val()},
            type: "POST",
            success: function(data){ 
                $("#traineeList").html(data);
                $("#subTotal").html("Total: " + $('#traineeList tr').length.toString());
                document.getElementById("classHour").defaultValue = "4";
            }
            });
         });

    });

</script> 

how do I pass the array in 'list_ByBatch' to view page which is already loaded when #batch-list change function execute.

Upvotes: 0

Views: 802

Answers (1)

taxicala
taxicala

Reputation: 21759

Instead of returning, you should be using echo and json_encode:

public function list_ByBatch(){
    $batch=$this->input->post('batchid',TRUE);      
    $list['trainee']=$this->AttendanceModel->get_traineeList($batch);
    if(!empty($list['trainee'])){
        echo json_encode($list['trainee']);
    }
}

Upvotes: 2

Related Questions