Skip to content Skip to sidebar Skip to footer

How To Make A Switch Button Post To The Db When Clicked

I am trying to figure out how to make a toggle switch button post to the db when it is pressed, so that it can update the info: yes or no. Like a submit button. This is in the view

Solution 1:

You could try something like this:

<divclass="switch_options"for='<?phpecho$Employee_Number;?>'><labelclass="col-sm-5 control-label">Complete: </label><spanval='true'class="switch switch_enable"> Yes </span><spanval='false'class="switch switch_disable"> NO </span></div><script>
$('.switch').click(function(){
  var id = $(this).parent().attr('for');
  var complete = $(this).attr('val');
  var that = this;
   $.ajax({
        url: "<?phpecho base_url();?>edit_complete",
        type: "post",
        data: 'complete='+complete+'id='+id,
        success: function(data){
            if (data.indexOf("Edit complete OK") >= 0){
              $(that).addClass('selected');
              $(that).siblings('.switch').removeClass('selected');
            }
        }
    });
});
</script>

You will need a controller to work your database query.Here it is:

publicfunctionedit_complete() { 
   if($this->session->userdata('logged_admin')){ 
      $complete_value = $this->input->post('complete');
      $Employee_Number = $this->input->post('id');
      if($this->callin_model->edit_complete($Employee_Number, $complete_value)){
        echo'Edit complete OK';
      }
   }
 }

And inside your model:

functionedit_complete($Employee_Number,$complete_value){
  $data = array('Complete' => $complete_value);
  $this->db->where('Employee_Number', $Employee_Number);
  $this->db->update('mytable_name', $data);
  if($this->db->affected_rows()==1)returnTRUE;
  returnFALSE;
}

Post a Comment for "How To Make A Switch Button Post To The Db When Clicked"