Welcome Guest, Not a member yet? Register   Sign In
Having a problem with my drop down dependency
#1

I am having a hard time figuring out how to make a make a drop down dependency(payment terms base on selected class name), when you select the name  class in the selectbox the select box generate a payment terms option base on the class select box base on the sql table (see last table). I think there is something wrong with my java script and CI model. I am very new in codeigniter activerecord, Please help me with my concern and Thank you very much in advance

P.S I dont think if my CI model and javascript is correct or maybe something is missing.

CLASS TABLE
Code:
 classes
     |-------|------------------|
     |  id   |       Name       |
     |-------|------------------|
     |   1   |   Grade 1        |
     |-------|------------------|
     |   2   |   Grade 2        |
     |-------|------------------|

    
PAYMENT TERMS TABLE
Code:
payment_terms
     |--------|------------------------------------|
     |   id   |       payment_terms                |
     |--------|------------------------------------|
     |   1    |   Grade 1    Full Payment          |
     |--------|------------------------------------|
     |   2    |   Grade 1    Quarterly Payment     |
     |--------|------------------------------------|
     |   3    |   Grade 2    Full Payment          |
     |--------|------------------------------------|
     |   4    |   Grade 2    Quarterly Payment     |
     |--------|------------------------------------|


ACADEMIC YEAR TABLE   
PHP Code:
academic_year 
      
|--------|------------------------------------|
        id         academic_year                |
      |--------|------------------------------------|
        1            2018-2019                  |
      |--------|------------------------------------| 


ACADEMIC YEAR AND PAYMENT TERMS TABLE
Code:
academic_year_payment_terms
      |--------|--------------------------------|------------------|
      |   id   | academic_year_id               | Payment_term_id  |
      |--------|--------------------------------|------------------|
      |   1    | 1                              | 1                |
      |--------|------------------ -------------|------------------|
      |   2    | 1                              | 2                |
      |--------|--------------------------------|------------------|
      |   3    | 1            .                 | 3                |
      |--------|--------------------------------|------------------|
      |   4    | 1                              | 4                |
      |--------|--------------------------------|------------------|
 

ACADEMIC YEAR PAYMENT TERMS AND CLASS TABLE
Code:
academic_year_payment_terms_class
     |--------|-----------------------------|------------------|
     |   id   | academic_year_payment_terms | class_id         |
     |--------|-----------------------------|------------------|
     |   1    | 1                           | 1                |
     |--------|-----------------------------|------------------|
     |   2    | 2                           | 1                |
     |--------|-----------------------------|------------------|
     |   3    | 3                           | 2                |
     |--------|-----------------------------|------------------|
     |   4    | 4                           | 2                |
     |--------|-----------------------------|------------------|

 

My Codeigniter View
PHP Code:
<div class="form-group">
 
     <select  id="class_id" name="class_id" class="form-control" >
 
        <option value=""><?php echo $this->lang->line('select'); ?></option>
             <?php
                foreach 
($classlist as $class) { ?>
               <option value="<?php echo $class['id'?>"<?php
                   
if (set_value('class_id') == $class['id']){
 
                       echo "selected =selected";
 
                  }
 
                  ?>><?php echo $class['class'?></option>
                <?php
                   $count
++;
 
               }
 
               ?>
      </select>
    </div>
    <div class="form-group">
      <select class="form-control" id="academic_year_payment_terms_id" name="academic_year_payment_terms_id">
      </select>
    </div> 

My JavaScript
Code:
<script type="text/javascript">
   function getAcademicPaymentTermsByClass(class_id, academic_year_payment_terms_id {
       if (class_id != "" && academic_year_payment_terms_id != "") {
           $('#academic_year_payment_terms_id').html("");
           var base_url = '<?php echo base_url() ?>';
           var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
           $.ajax({
               type: "GET",
               url: base_url + "enrollment/fetchPaymentTermsByClass",
               data: {'class_id': class_id},
               dataType: "json",
               success: function (data) {
                   $.each(data, function (i, obj)
                   {
                       var sel = "";
                       if (academic_year_payment_terms_id == obj.academic_year_payment_terms_id) {
                           sel = "selected";
                       }

                       div_data += "<option value=" + obj.academic_year_payment_terms_id+ " " + sel + ">" + obj.payment_terms + "</option>";
                   });
                   $('#academic_year_payment_terms_id').append(div_data);
               }
           });
       }
   }

   $(document).ready(function () {
       var class_id = $('#class_id').val();
       var academic_year_payment_terms_id = '<?php echo set_value('academic_year_payment_terms_id') ?>';
       getAcademicPaymentTermsByClass(class_id, academic_year_payment_terms_id);

       $(document).on('change', '#class_id', function (e) {
           $('#academic_year_payment_terms_id').html("");
           var class_id = $(this).val();
           var base_url = '<?php echo base_url() ?>';
           var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
           $.ajax({
               type: "GET",
               url: base_url + "enrollment/fetchPaymentTermsByClass",
               data: {'class_id': class_id},
               dataType: "json",
               success: function (data) {
                   $.each(data, function (i, obj)
                   {

                       div_data += "<option value=" + obj.academic_year_payment_terms_id' + ">" + obj.payment_terms' + "</option>";
                   });
                   $('#academic_year_payment_terms_id').append(div_data);
               }
           });
       });
   });
</script>

My Controller
PHP Code:
function fetchPaymentTermsByClass() {
 
       $class_id $this->input->get('class_id');
 
       $data $this->Enrollment_model->getPaymentTermsByClass($class_id);
 
       echo json_encode($data);
 
      

My model
PHP Code:
      public function getPaymentTermsByClass($classid){
 
       $this->db->select('academic_year_payment_terms_class.id, academic_year_payment_terms_class.academic_year_payment_terms_id, academic_year_payment_terms.payment_terms');
 
       $this->db->from('academic_year_payment_terms_class');
 
       $this->db->join('academic_year_payment_terms''academic_year_payment_terms.id = academic_year_payment_terms_class.academic_year_payment_terms_id');
 
       $this->db->where('academic_year_payment_terms_class.class_id'$classid);
 
       $this->db->order_by('academic_year_payment_terms_class.id');
 
       $query $this->db->get();
 
       return $query->result_array();
 
       
Reply
#2

in your ajax query you handling only success status, 
you should also handle fail statuses, 
$.ajax()
.fail(function(xhr){
console.log(xhr);
})

log in console the response data from ajax and check if your CI controller returning valid value
Reply
#3

Duplicate thread. See https://forum.codeigniter.com/thread-71254.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB