update status from active to inactive using codeigniter

10,253

This is sample code, This should work

Controller:

public function update_status(){

    $status = $this->input->post('status');
    $course_id = $this->input->post('id');
    $this->CoursesModel->update_course_status($course_id,$status);
}

Model:

public function update_course_status($course_id,$status){
  $data['status'] = $status;
  $this->db->where('course_id', $course_id);
  $this->db->update('courses',$data);
}

Script:

$(document).on('click','.status_checks',function()
 { 

var status = ($(this).hasClass("btn-success")) ? '1' : '0'; 
var msg = (status=='0')? 'Deactivate' : 'Activate'; 
if(confirm("Are you sure to "+ msg))
{ 
    var current_element = $(this); 
    var id = $(current_element).attr('data');
    url = "<?php echo base_url().'index.php/Dashboard/update_status'?>"; 
        $.ajax({
          type:"POST",
          url: url, 
          data: {"id":id,"status":status}, 
          success: function(data) { 
          // if you want reload the page
          location.reload();
          //if you want without reload
          if(status == '1'){
            current_element.removeClass('btn-success');
            current_element.addClass('btn-danger');
            current_element.html('Deactivate');
          }else{
            current_element.removeClass('btn-danger');
            current_element.addClass('btn-success');
            current_element.html('Activate');
          }
    } });
 }  
 });

HTML:

<button type="button" class="status_checks btn <?php echo ($element->status == 1) ? "btn-danger" : "btn-success"; ?> "><?php echo ($element->status == 1) ? "Deactivate" : "Activate"; ?></button>
Share:
10,253
ohmygood
Author by

ohmygood

Updated on June 08, 2022

Comments

  • ohmygood
    ohmygood about 2 years

    Hi when i want to update my status using a button from inactive to active through ajax method so i have written some code i dont know wether it is right or wrong but status is not updating

  • KUMAR
    KUMAR over 2 years
    please edit your answer & add code of view button.
  • Dilip Patel
    Dilip Patel over 2 years
    @KUMAR Answer updated.
  • KUMAR
    KUMAR over 2 years
    its done but it reloads the page.
  • Dilip Patel
    Dilip Patel over 2 years
    @KUMAR Answer updated.