update status value in backend while click on button in codeigniter

15,093

Solution 1

Check this:

View

<input type="hidden" name="candidate_id" value="<?php echo $candidate_id; ?>"/>//your candidate_id
<button type="button" id="button" class="btn btn-info" >
    <b><?php $status == 0? 'REQUEST CONTACT INFO':'FINISHED';?></b>//your status value
</button>

<script src="<?php echo base_url(); ?>assets/plugins/jquery.min.js"></script>

js

$('#button').click(function() {
    var candidate_id = $('input[name="candidate_id"]').val();
    var url = 'your_cntroller_name/change_status/';
    $.ajax({
        url: your_base_url + url,
        type: 'POST',
        data: {'$candidate_id': $candidate_id},
        dataType: 'JSON',
        success: function(data) {    
             $('#button').text('FINISHED');
        }
    });
});

Controller

public function change_status() {
    $candidate_id = $this->input->post('candidate_id');
    $this->your_model->update_status($candidate_id);

    echo true;
    exit;
}

Model

public function update_status($candidate_id) {

    //your query toupdate status

}

Solution 2

complete step by step solution is step -1 in your view

<td><button type="button" id="button" class="btn btn-info" onclick="getConfirmation(row id);"><b>REQUEST CONTACT INFO</b></button></td>

getConfirmation(id){
$.ajax({
        url: "url_to_your_controller/change_status",
        type: "post",
        data: id,
        success: function (response) {
         location.reload();              

        }
    });
}

step -2 in your controller

change_status(){
$id = $this->input->post('id');
$status = $this->db->query("Your query")->row()->status;
if($status==1){
    $status = 0;
} else {
    $status = 1;
}
$data=array('status'=>$status);
$this->db->where('id','id');
$this->db->update('table_name',$data);
}
Share:
15,093
M5533
Author by

M5533

Updated on June 04, 2022

Comments

  • M5533
    M5533 about 2 years

    In view_candidates form in my application, I have a button with text REQUEST CONTACT INFO. When I click on the button, the text will be changed automatically to FINISH.

    Now what happens: When I refresh the page the button text automatically changes to REQUEST CONTACT INFO.

    To overcome this, I gave a status column in my database.

    What I want:

    Once user click on the button, the status should change from 0 to 1.

    With status, I want to display my button text like: if status=0 button should be REQUEST CONTACT INFO, else it should be FINISH.

    Button code:

    <td>
        <button type="button" id="button" class="btn btn-info" onclick="getConfirmation(id);">
            <b>REQUEST CONTACT INFO</b>
        </button>
    </td>
    

    SCRIPT:

    <script>
         function  getConfirmation(id)
         {
    
            var retVal = confirm("your request is confiremed ?");
            if(retVal)
                            {
    
                              $('#button').text('Finish');
    
            $.ajax({
                    url: "Candidate/user_view_candidates/change_status",
                    type: "post",
                    data: id,
                    success: function (response) 
                    {
                     //location.reload();              
                     alert('success');
                    }
    
                });
            }
        }
    
        </script>
    

    Controller code:

        public function  change_status()
    
     { 
    
         $id = $this->input->post('candidate_id');
         $status = $this->db->query("select status from candidates_details          where id = candidate_id")->row()->status;
        if($status==0)
        {
            $status = 1;
        } 
        else 
        {
            $status = 0;
        }
    
        $data=array('status'=>$status);
        $this->db->where('candidate_id',$id);
        $this->db->update('candidates_details',$data);
    }
    

    Can someone help me?
    Thanks in advance.