如何在单击时使切换按钮发布到数据库

How to make a switch button post to the db when clicked

本文关键字:按钮 数据库 单击      更新时间:2023-09-26

我正在尝试弄清楚如何在按下数据库时向数据库发布切换开关按钮,以便它可以更新信息:。就像一个提交按钮。这是我的编码点火器应用程序的视图。

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('.switch_options').each(function() {
        //This object
        var obj = jQuery(this);
        var enb = obj.children('.switch_enable'); //cache first element, this is equal to ON
        var dsb = obj.children('.switch_disable'); //cache first element, this is equal to OFF
        var input = obj.children('input'); //cache the element where we must set the value
        var input_val = obj.children('input').val(); //cache the element where we must set the value
        /* Check selected */
        if( 'NO' == input_val ){
            dsb.addClass('selected');
        }
        else if( 'Yes' == input_val ){
            enb.addClass('selected');
        }
        //Action on user's click(ON)
        enb.on('click', function(){
            $(dsb).removeClass('selected'); //remove "selected" from other elements in this object class(OFF)
            $(this).addClass('selected'); //add "selected" to the element which was just clicked in this object class(ON) 
            $(input).val('Yes').change(); //Finally change the value to 1
        });
        //Action on user's click(OFF)
        dsb.on('click', function(){
            $(enb).removeClass('selected'); //remove "selected" from other elements in this object class(ON)
            $(this).addClass('selected'); //add "selected" to the element which was just clicked in this object class(OFF) 
            $(input).val('NO').change(); // //Finally change the value to 0
        });
    });
});

</script>

切换开关 HTML 代码

<div class="switch_options">
 <label class="col-sm-5 control-label">Complete: </label>
    <span class="switch_enable"> Yes </span>
    <span class="switch_disable"> NO </span>
    <input type="hidden" class="default"value="<?php echo $e->Complete;?>">
    <input type="hidden" name="Complete" class="switch_val" value=""/>
</div>

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Callin extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->model('callin_model');
        }   
    //Shows the dashboard
    public function index()
    {
         if($this->session->userdata('logged_admin'))
        {      
        $this->load->view('templates/admin_header');
        $this->load->view('insert_callins_view');

       }else{
           redirect('admin_authentication/admin_login_show');
        }
    }
    //Insert the callin 
    public function  insert_callin()
    { 
 if($this->session->userdata('logged_admin'))
        {     
    $data=array('Date_Scheduled'=>$this->input->post('Date_Scheduled'),
            'Employee_Name'=>$this->input->post('Employee_Name'),
            'Employee_Number'=>$this->input->post('Employee_Number'),
            'Time_Reported'=>$this->input->post('Time_Reported'),
            'Reason'=>$this->input->post('Reason'),
            'Scheduled_Area'=>$this->input->post('Scheduled_Area'),
            'Contact'=>$this->input->post('Contact'),
            'Approval'=>$this->input->post('Approval'),
            'Complete'=>$this->input->post('Complete'),
            'status'=>1);
        //print_r($data);
            $result=$this->callin_model->insert_callin($data); 
            if($result > 0) 
        {
            $this->session->set_flashdata('msg',"Callin Record Added Successfully");
            redirect('callin');
        }
        else
        {
            $this->session->set_flashdata('msg1',"Callin Record Added Failed");
            redirect('callin');

        }
         }else{
           redirect('admin_authentication/admin_login_show');
        }
    }
    //List of callins 
        public function list_callins()
    {

         if($this->session->userdata('logged_admin'))
        {  

            $data['callin'] =$this->callin_model->get_callin();
            $this->load->view('templates/admin_header');
            $this->load->view('admin_callins_view',$data);
               }else{
           redirect('admin_authentication/admin_login_show');
        }

    }
    //List of callins
        public function viewlist_callins()
    {
           if($this->session->userdata('logged_in'))
        {  
            $data['callin']=$this->callin_model->get_callin();
            $this->load->view('templates/header');
            $this->load->view('user_callins_view',$data);

               }else{
           redirect('user_authentication/user_login_show');
        }
    }
    public function delete_callin()
    {
        $id=$this->input->post('id');
        $data=array('status'=>0);
        $result=$this->callin_model->delete_callin($id,$data);
        if($result==true)
        {
            $this->session->set_flashdata('msg1',"Deleted Successfully");
            redirect('callin/list_callins');
        }
        else
        {
            $this->session->set_flashdata('msg1',"callin Record Deletion Failed");
            redirect('callin/list_callins');

        }
    }

    public function edit_callin()
    {
        $id=$this->uri->segment(3);
        $data['callin']=$this->callin_model->edit_callin($id);
        $this->load->view('templates/admin_header',$data);
        $this->load->view('edit_callin');

    }
    public function  update_callin()
    {

        $id=$this->input->post('id');
        $data=array('Date_Scheduled'=>$this->input->post('Date_Scheduled'),
            'Employee_Name'=>$this->input->post('Employee_Name'),
            'Employee_Number'=>$this->input->post('Employee_Number'),
            'Time_Reported'=>$this->input->post('Time_Reported'),
            'Reason'=>$this->input->post('Reason'),
            'Scheduled_Area'=>$this->input->post('Scheduled_Area'),
            'Contact'=>$this->input->post('Contact'),
            'Approval'=>$this->input->post('Approval'),
            'Complete'=>$this->input->post('Complete'));
            //print_r($data);   
        $result=$this->callin_model->update_callin($data,$id);
        if($result)
        {
            $this->session->set_flashdata('msg',"Callin Record Updated Successfully");
            redirect('callin/list_callins');

        }
        else
        {
            $this->session->set_flashdata('msg1',"No changes Made in Callin Record");
            redirect('callin/list_callins');

        }
    }
}
?>

<?php
class Callin_Model extends CI_Model 
{
    public function insert_callin($data)
    {
    $this->db->insert('callin_list',$data); 
return $this->db->insert_id(); 
    }
    public function get_callin()
    {
        $this->db->select('*');
        $this->db->from('callin_list');
        $this->db->where('status',1);
        $this->db->order_by("id", "desc");
        $this->db->order_by("Scheduled_Area", "desc");
        $this->db->order_by("Reason", "desc");
        $query =$this->db->get();
        return $query->result();

    } 
    public function delete_callin($id,$data)
    {
        $this->db->where('id',$id);
        $this->db->update('callin_list',$data);
            return print_r($data);

    }
    public function edit_callin($id)
    {
        $this->db->select('*');
        $this->db->from('callin_list');
        $this->db->where('id',$id);
        $this->db->where('status',1);
        $query =$this->db->get();
        return $query->result();
    }
    public function update_callin($data,$id)
    {
        $this->db->where('id',$id);
        $this->db->update('callin_list',$data);
        return print_r($data);
    }
}

你可以尝试这样的事情:

<div class="switch_options" for='<?php echo $Employee_Number;?>'>
 <label class="col-sm-5 control-label">Complete: </label>
    <span val='true' class="switch switch_enable"> Yes </span>
    <span val='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: "<?php echo 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>

您将需要一个控制器来处理数据库查询。在这里:

 public function  edit_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';
      }
   }
 }

在您的模型中:

function edit_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)return TRUE;
  return FALSE;
}