CodeIgniter更新查询无效

时间:2016-06-01 10:20:47

标签: php mysql ajax codeigniter

我想从系统中终止员工。当点击终止按钮时,它会弹出一个moadal,询问是要终止还是取消。如果终止数据库值resign应该更新为0,但是现在按钮不起作用。

这是我的代码

控制器

public function ajax_list()
{
    $list = $this->employees->get_datatables();

    $data = array();
    $no = $_POST['start'];
    foreach ($list as $emp) {
        $no++;
        $row = array();
        $row[] = $emp->employee_id;
        $row[] = $emp->name;
        $jid = $emp->job_title;
        $desigdata = $this->employees->GetJobTitlebyID($jid);
        $row[] = $desigdata->desc;
        $did = $emp->department;
        $deptdata = $this->employees->GetDepartmentbyID($did);
        $row[] = $deptdata->title;
        $secid = $emp->section;
        $secdata = $this->employees->GetSectionbyID($secid);
        $row[] = $secdata->desc;


        //add html for action
        $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void()"  onclick="terminate_emp('."'".$emp->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Terminate</a>';


        $data[] = $row;
    }

    $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->employees->count_all(),
        "recordsFiltered" => $this->employees->count_filtered(),
        "data" => $data,
    );

    echo json_encode($output);
}


public function ajax_terminate()
    {
        $this->_validate();
        $data = array(
            'resign' => $this->input->post('resign'),

        );
        $this->employees->update(array('id' => $this->input->post('id')), $data);
        echo json_encode(array("status" => TRUE, "id" => $this->input->post('id')));
    }

模型

 function terminate_emp($data)
 {
     $this->db->where('resign', 0);
     $this->db->update('employees', $data);
 }

查看

function terminate_emp(id)
    {
        save_method = 'update';
        $('#form')[0].reset(); 
        $('.form-group').removeClass('has-error'); 
        $('.help-block').empty(); 

        //Ajax Load data from ajax
        $.ajax({
            url : "<?php echo site_url('employees_con/ajax_terminate/')?>/" + id,
            type: "GET",
            dataType: "JSON",
            success: function(data)
            {
                $('[name="id"]').val(data.id);

                if(data.resign == 1)
                {
                    //$('[name="resign"]').val(data.resign);
                    $('#resign').prop('checked', true);
                }
                $('[name="resign"]').val(data.resign);

                $('#modal_formterminate').modal('show'); // show bootstrap modal when complete loaded
                $('.modal-title').text('Terminate Employee'); // Set title to Bootstrap modal title

            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error get data from ajax');
            }
        });
    }



<div class="modal fade" id="modal_formterminate" role="dialog">

    <div class="modal-dialog modal-full" style="max-width: 600px">

    <div class="modal-content">

        <div class="modal-header bg-blue-steel bg-font-blue-steel">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h3 class="modal-title bold uppercase">Person</h3>
        </div>

        <div class="modal-body form">
            <form action="#" id="form" class="form-horizontal">
                <input type="hidden" value="" name="id"/>
                <div class="form-body">

                    <div id="empWizard">


                        <p style="color: #0000cc"><b>Are You sure to Terminate this employee</b></p>

                    </div>
                </div>

            </form>
        </div>

        <div class="modal-footer">
            <button type="button" id="btnSaveterminate" onclick="save()" class="btn btn-primary">Terminate</button>
            <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
        </div>

    </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->

</div>

2 个答案:

答案 0 :(得分:0)

在控制器中

$this->employees->update(array('id' => $this->input->post('id')), $data);

您正在传递两个参数来更新模型的功能,一个是数组,另一个$data也是一个数组。

但在模特中,

function terminate_emp($data)
{
    $this->db->where('resign', 0);
    $this->db->update('employees', $data);
}

您只是接受更新功能中的一个参数。

答案 1 :(得分:0)

首先,您通过AJAX和Controller函数GET发出ajax_terminate()请求,您正在使用POST访问变量。您resign值未通过ajax并且您正在尝试使用控制器函数ajax_terminate()。见下面的代码: -

function terminate_emp(id)
{
    save_method = 'update';
    $('#form')[0].reset(); 
    $('.form-group').removeClass('has-error'); 
    $('.help-block').empty(); 

    //Ajax Load data from ajax
    $.ajax({
        url : "<?php echo site_url('employees_con/ajax_terminate/')?>/",
        type: "POST",
        dataType: "JSON",
        data: {id:id,resign:YOUR_RESIGN_VALUE}
        success: function(data)
        {
            $('[name="id"]').val(data.id);

            if(data.resign == 1)
            {
                //$('[name="resign"]').val(data.resign);
                $('#resign').prop('checked', true);
            }
            $('[name="resign"]').val(data.resign);

            $('#modal_formterminate').modal('show'); // show bootstrap modal when complete loaded
            $('.modal-title').text('Terminate Employee'); // Set title to Bootstrap modal title

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error get data from ajax');
        }
    });
}

将模型更改为

function terminate_emp($where,$data)
{
    $this->db->where($where);
    $this->db->update('employees', $data);
}