简体   繁体   中英

disable select option if the option value is already exist

I am creating a library membership form and in this, form the student list are populated through ajax request in select option. Now i need to disable the student options who are already a library member.

Form View

<div class="form-group">
  <div class="input-group">
    <span class="input-group-addon"><i class="fa fa-asterisk" aria-hidden="true"></i> <i class="fa fa-calendar" aria-hidden="true"></i>  <?php echo get_phrase('_batch'); ?></span>
    <select id="batch_result_holder" name="batch_result_holder" onchange="return get_batchs_students(this.value)" data-plugin="select2" required>
      <option disabled selected value=""> <?php echo get_phrase('select_programs_first'); ?></option>
    </select>
  </div>
</div><!-- form-group -->

<div class="form-group">
  <div class="input-group">
    <span class="input-group-addon"><i class="fa fa-asterisk" aria-hidden="true"></i> <i class="fa fa-user" aria-hidden="true"></i>  <?php echo get_phrase('_student'); ?></span>
    <select id="student_result_holder" name="student_result_holder"  data-plugin="select2" >
      <option disabled selected> <?php echo get_phrase('select_batch_first'); ?></option>
    </select>
  </div>
</div><!-- form-group -->

AJAX CODE to Pull the Student Info

function get_batchs_students(batch_result_holder){
  var program_id = $('#program_id').val();
  $.ajax({
    type:"POST",
    url: '<?php echo base_url();?>index.php?admin/get_batch_students_without_assigned/',
    data:{batch_result_holder: batch_result_holder, program_id:program_id},

    success: function(response)
    {
      jQuery('#student_result_holder').html(response);
    }
  });
}

CONTROLLER which pull the student list

function get_batch_students_without_assigned($program_id, $batch_id, $status){
  $program_id = $this->input->post('program_id');
  $batch_id = $this->input->post('batch_result_holder');
  $students = $this->crud_model->get_student_list_without_section($program_id, $batch_id, 1);
  $assigned_student = $this->crud_model->get_assigned_students();
  echo '<option value="" selected disabled> select from list below </option>';
  foreach($assigned_student as $row2):
    foreach($students as $row){

      echo '<option value="' . $row['student_id'] . '"';
      if($row['student_id'] == $row2['type_id']):
        echo 'disabled';
      endif;
      echo '>';
      echo $row['name'];
      echo '</option>';
    }
  endforeach;
}

But the above controller will loop through all the students and assigned students and outputs same student options on disabled and non-disabled format. So, how do i prevent this controller to show the all students are disabled students who are already a member.

output of the above controller

You can collect the assigned student's ids in an array, and check whether the student_id in the loop is in the array:

  $assigned = array();
  foreach($assigned_student as $row2) {
    $assigned[] = $row2['student_id'];
  }
  foreach($students as $row){}
      echo '<option value="' . $row['student_id'] . '"';
      if(in_array($row['student_id'], $assigned):
        echo 'disabled';
      endif;
      echo '>';
      echo $row['name'];
      echo '</option>';
  endforeach;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM