繁体   English   中英

使用复选框更新多个数据codeigniter

[英]update multiple data codeigniter with checkbox

我正在尝试通过AJAX更新多个数据,但是我已经尝试了几次,但是数据没有更改,请帮助修复它以便完美运行。

控制器:

function update_notif() {
    $this->My_model->update_multiple();
    $this->session->set_flashdata('msg', 
                '<div class="callout callout-warning">
                    <h4>Success</h4>
                </div>');                   
}

楷模:

function update_multiple() {
    $update = $this->input->post('id_pesan');
    for ($i=0; $i < count($update) ; $i++) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $delete[$i]);
        $this->db->update('my_table', $data);               
    }
}   

观看次数:

<button type="button" id="btn_delete" ></button>

<table class="table table-hover table-striped">
    <thead>
        <tr>
            <td><input id="selecctall" type="checkbox"></td>
            <td>No</td>
            <td>Name</td>
            <td>Telp</td>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($show_data as $row) { ?>
        <tr>
            <td><input type="checkbox" class="select_update" name="select_update[]" value="<?php echo $row->id; ?>"></td>
            <td><?php echo $row->no; ?></td>
            <td><?php echo $row->nama; ?></td>
            <td><?php echo $row->telp; ?></td>
        </tr>   
    </tbody>
    <?php } } ?>
</table>    

阿贾克斯:

var getVar = [];  
$(".select_update:checked").each(function() {  
        getVar.push($(this).val());
}); 

$.ajax({
    url : url,
    type: 'POST',
    data: 'id_pesan='+getVar,
    dataType: 'json',
    success: function(response)
    {
        if(response){
            location.reload();
        }           
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error');
    }
 });

您的Ajax看起来如何?
我认为你必须改变

$update = $this->input->post('select_id');

进入

$update = $this->input->post('select_update'); $this->input->post('select_update[]');

由于您的复选框的name属性和更改

$this->db->where('id', $delete[$i]);

进入

$this->db->where('id', $update[$i]);

或在foreach使用它

function update_multiple() {
    $updates = $this->input->post('select_update');

    foreach ($updates as $update) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $update);
        $this->db->update('my_table', $data);               
    }
}   

更新#1

你阿贾克斯应该是这样的-保留范围的dataajax

$.ajax({
  url : url,
  type: 'POST',
  data: $(".select_update:checked").serialize(),
  dataType: 'json',
  success: function(response) { ... },
  error: function (jqXHR, textStatus, errorThrown) { ... }
});

看来您已经从delete_multiple方法粘贴了代码,这就是为什么在这里使用$delete而不是$update

function update_multiple() {
    $update = $this->input->post('select_id');
    for ($i=0; $i < count($update) ; $i++) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $delete[$i]);
        $this->db->update('my_table', $data);               
    }
}

解:

function update_multiple() {
    $update = $this->input->post('select_id');
    for ($i=0; $i < count($update) ; $i++) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $update[$i]);
        $this->db->update('my_table', $data);               
    }
}

受到UfguFugullu的启发,他显然也受到我的回答的启发:

select_update需要,而不是select_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM