如何在单击同一按钮时更新表数据?

时间:2018-12-12 09:20:24

标签: php jquery codeigniter

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            uid = $("input[name='user_id']:checked").map(function() {
                    return this.id;
                }).get().join(",");
            $.ajax({
                type:"POST",
                data:{"uid":uid},
                url:"<?php echo base_url(); ?>hr/shortlist",
                success:function(data){
                    setTimeout(function(){
                        location.reload();
                    }, 1000);
                }
            });
        });
    });
</script>

<table>
    <thead>
        <tr class="info">
            <th>Check</th>
            <th>Recruiter Id</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="checkbox" name="user_id" id="uid1211120937" class="user_id">
            </td>
            <td>20181123091338</td>
        </tr>
        <tr>
            <td>
                <input type="checkbox" name="user_id" id="uid1211092847" class="user_id">
            </td>
            <td>20181123091338</td>
        </tr>       
    </tbody>
</table>
<input type="submit" name="submit" id="submit" value="short"/>

shortlist.php

$uid = explode(",",$this->input->post('uid'));
foreach($uid as $user_id)
{
    $data = array('shortlist'=>'1');
    $this->db->where('uid',$user_id);
    $query = $this->db->update('personal_detail',$data);
}

在这个问题上,我有多个复选框。现在,如果我选择了所有复选框然后shortlist must be 1 for all,我想要的东西将被更新;如果我未选中某些复选框,则uncheck checkbox value will be 0我将要更新的东西。那么,我该怎么做?请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

您需要找到与用户ID相关的所有复选框状态。

$(document).ready(function(){
    $("#submit").click(function(e){
        e.preventDefault();

        var uids = [];

        $("input[name='user_id']").map(function() {
            uids.push({id: this.id, value: this.checked ? 1 : 0});
        });

        $.ajax({
            type:"POST",
            data:{"uid":uids},
            url:"<?php echo base_url(); ?>hr/shortlist",
            success:function(data){
                setTimeout(function(){
                    location.reload();
                }, 1000);
            }
        });
    });
});

现在在 shortlist.php

$uid = $this->input->post('uid');

foreach($uid as $user)
{
    $data = array('shortlist'=> $user['value']);
    $this->db->where('uid',$user['id']);
    $query = $this->db->update('personal_detail',$data);
}