title: How to change 'active/inactive status of user' through ajax?
body: <p>I am display list of users in datatable based on 2 parameters through AJAX in codeigniter. I want to change the status of user in database and display in table. I have 3 status 0-> inactive, 1->active, -1->left. I want to change the status and display in datatable without reloading the page. </p>
  
  <p>I have tried changing the status but the status is changing only once. After first AJAX call there is no change when i again change the status. </p>
  
  <pre><code>//view
  
      &lt;script&gt;
      $(document).ready(function()
      {
      $('#academicTable_wrapper').hide();
      $('#studentTable').DataTable();
      showStudents();
      function showStudents()
      {
          $('#submitBtn').on('click',function(){
              var courseId = $('#courseId').val();
        var classId = $('#classId').val();
        $.ajax({
                  type  : 'POST',
                  url   : "&lt;?php echo base_url();?&gt;Student/getStudentsList",
                  // async : true,
                  data  : {courseId:courseId,classId:classId},
                  dataType : 'json',
                  success : function(data){
                    //alert(data);
                      var html = '';
                      var i;
  
                      for(i=0; i&lt;data.length; i++){
                         var studentId = data[i].studentId;
  
                          if(data[i].status == 1)
                            var status = "Approved";
                          else if(data[i].status == 0)
                            var status = "Pending";
                          else
                            var status = "Left";
                          html += '&lt;tr&gt;'+
                                  '&lt;td&gt;'+data[i].studentId+'&lt;/td&gt;'+
                                  '&lt;td&gt;'+data[i].studentName+'&lt;/td&gt;'+
                                  '&lt;td&gt;'+data[i].studentPhoneNum+'&lt;/td&gt;'+
                                  '&lt;td&gt;'+data[i].created_on+'&lt;/td&gt;'+
                                  '&lt;td id="changeStatus"&gt;'+status+'&lt;/td&gt;'+
                                  '&lt;td&gt;'+
  
                        '&lt;a href="'+'viewStudentProfile/'+studentId+'/'+classId+'/'+courseId+'/'+data[i].programId+'" class="btn btn-secondary btn-sm text-white"&gt;View&lt;/a&gt;'+
                        ' '+
                        '&lt;a id="activateBtn" data-id="'+data[i].id+'" data-status="'+data[i].status+'" class="btn btn-primary btn-sm text-white"&gt;Activate/Deactivate&lt;/a&gt;'+
                        ' '+
                        '&lt;a href="javascript:void(0);" class="btn btn-danger btn-sm item_delete"&gt;Delete&lt;/a&gt;'+
                                  '&lt;/td&gt;'+
                                  '&lt;/tr&gt;';
                      }
                      $('#studentTable').DataTable().destroy();
                      $('#showData').html(html);
                      $('#studentTable').DataTable();
                      $('#academicTable_wrapper').show();
                  }
  
              });        
        });
      }
  
  
  
      $(document).on('click','#activateBtn',function()
      {
        var id = $(this).data('id');
        var status = $(this).data('status');
        $.ajax({
  
          method: 'POST',
          url: "&lt;?php echo base_url();?&gt;Student/approveStudent",
          data:{id:id,status:status},
          success : function(data)
          {
            alert(data);
            $('#changeStatus').text('');
            if(data == 0)
            {
              showStudents();
              $('#changeStatus').html('Inactive');  
            }
            else{
              showStudents();
              $('#changeStatus').html('Approv');  
            }
          },
          error:function(data)
          {
            console.log(data);
          }
        },1000);
  
      });
  
    });
  &lt;/script&gt;
  
  
      //controller
  
      function approveStudent()
      {
          $id = $this-&gt;input-&gt;post('id');
          $status = $this-&gt;input-&gt;post('status');
          $col = 'id';
          $query = $this-&gt;Admin_model-&gt;activate($col,$id,$status,$this-&gt;studentDetail);
  
          if($query){
  
              $result = $this-&gt;Admin_model-&gt;getData($col,$id,$this-&gt;studentDetail);
          } 
  
           $status   = 1; 
           if ($result[0]-&gt;status == 0)
           {
              $status = 0;
           } 
           echo $status;
  
         }
  </code></pre>
  
  <p>I expect whenever I click <code>activateBtn</code> the status should change in database and also in datatable.</p>