dimanche 15 octobre 2023

on checkbox select dynamically display checkboxes in codeigniter php

i have a form where there are few checkboxes and when the user selects the checkboxes, some other checkboxes should display according to the selection,

$(document).ready(function() {
  // Handle branch checkbox change event
  $('.branch-checkbox').change(function() {
    var selectedBranches = [];

    // Get all selected branch values
    $('.branch-checkbox:checked').each(function() {
      selectedBranches.push($(this).val());

    });

    if (selectedBranches.length > 0) {
      // Send an AJAX request to fetch students based on selected branches
      $.ajax({
        url: "<?php echo base_url(); ?>index.php/homecontroller/fetchstudents",
        method: "POST",
        data: {
          selectedBranches: selectedBranches
        },
        success: function(data) {
          // Display the fetched students
          $('#student-container').html(data);
        }
      });
    } else {
      // Clear the student container if no branches are selected
      $('#student-container').empty();
    }
  });
});
<div class="col-md-12">
  <div class="form-group">
    <label style="border-bottom: 1px solid">Branch</label>
    <div class="row" id="branch-checkboxes"><input type="checkbox" class="branch-checkbox" value="5" name="branches[]"><label for="md_checkbox_5" style="margin-bottom:0px">Banjara Hills</label><br>
    </div>
  </div>
</div>


<div class="col-md-12">
  <div class="form-group">
    <label style="border-bottom: 1px solid">Students</label>
    <div class="row" id="student-container">
      <!-- Student checkboxes will be inserted here dynamically -->
    </div>
  </div>
</div>

below is my controller:

public function fetchstudents()
{
    if ($this->input->post('selectedBranches')) {
        $selectedBranches = $this->input->post('selectedBranches');
        $student_checkboxes = $this->category->fetch_student_checkboxes($selectedBranches);
        echo $student_checkboxes;
    }
}

and my model:

public function fetch_student_checkboxes($selectedBranches)
{
    $this->db->where_in('branch', $selectedBranches);
    $this->db->where('status', 'Active');
    $query = $this->db->get('register');

    $output = '';
    foreach ($query->result() as $row) {
        $output .= '<input type="checkbox" class="student-checkbox" value="' . $row->id . '" name="students[]">';
        $output .= '<label for="md_checkbox_' . $row->id . '" style="margin-bottom:0px">' . $row->name . '</label><br>';
    }

    return $output;
}

however this doesnt work and i dont get any error also, can anyone please tell me what could be wrong in here , thanks in advance




Aucun commentaire:

Enregistrer un commentaire