0

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.

1 Answer 1

2

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']);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

great, got it. but how do I receive this array in view page?
it's the first parameter of the success callback in your ajax call.
could you please help to do this. just how do I get the variable in my view page.
it the "data" parameter of your success callback in the ajax call "success: function(data){ "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.