【问题标题】:Update and Delete not automatically removed or updated after submit, Codeigniter and Ajax提交、Codeigniter 和 Ajax 后更新和删除不会自动删除或更新
【发布时间】:2020-03-21 05:05:32
【问题描述】:

我的更新问题是我可以更新我的数据,但我需要在更新之前先更改文本框的值。

我的删除问题是我可以删除我的数据,但我需要先刷新页面才能删除数据。

有什么解决办法吗?

我在其中循环和显示数据的视图。

// Show Groups
        function showGroups(){
            $.ajax({
                type: 'ajax',
                url: '<?php echo base_url() ?>groups/showGroups',
                async: false,
                dataType: 'json',
                success: function(data){
                    var html = '';
                    var i;
                    for(i=0; i<data.length; i++){
                        html +='<a class="text-decoration-none" href="<?php echo base_url() ?>groups/view_class/'+data[i].id+'">'+
                                    '<div class="col-md-4">'+
                                        '<div class="card" style="width: 15rem; text-align:center;display:inline-block;">'+
                                        '<div class="card-body">'+
                                            '<input type="hidden" name="groupId" value="1" />'+
                                            '<h5 class="card-title">'+data[i].group_name+'</h5>'+
                                            '<h6 class="card-subtitle mb-2 text-muted">Modify your Group</h6>'+
                                            '<a href="javascript:;" class="btn btn-info item-edit" data="'+data[i].id+'"><span class="iconify" data-icon="feather:edit" data-inline="false"></span></a>&nbsp;'+
                                            '<a href="javascript:;" class="btn btn-primary item-delete" data="'+data[i].id+'"><span class="iconify" data-icon="dashicons:remove" data-inline="false"></span></a>'+
                                        '</div>'+
                                        '</div><hr>'+
                                    '</div><hr>'+
                                '</a>';
                    }
                    $('#showdata').html(html);
                },
                error: function(){
                    alert('Could not get Data from Database');
                }
            });
        }

我的编辑显示我的模态并获取数据

//edit
    $('#showdata').on('click', '.item-edit', function(){
            var id = $(this).attr('data');
            $('#myModal').modal('show');
            $('#myModal').find('.modal-title').text('Edit Group');
            $('#myForm').attr('action', '<?php echo base_url() ?>groups/updateGroup');
            $.ajax({
                type: 'ajax',
                method: 'get',
                url: '<?php echo base_url() ?>groups/editGroup',
                data: {id: id},
                async: false,
                dataType: 'json',
                success: function(data){
                    $('input[name=group]').val(data.group_name);
                    $('input[name=txtId]').val(data.id);
                },
                error: function(){
                    alert('Could not Edit Data');
                }
            });
        });

我的更新

$('#btnSave').click(function(){
        var url = $('#myForm').attr('action');
        var data = $('#myForm').serialize();
        //validate form
        var group = document.getElementById('group').value;

        if(group.replace(/\s/g, "").length <=0 ) {
            swal("Submission fail!", "Enter the required field", "error");
            return false;
        }

            $.ajax({
                type: 'ajax',
                method: 'post',
                url: url,
                data: data,
                async: false,
                dataType: 'json',
                success: function(response){
                    if(response.success){
                        $('#myModal').modal('hide');
                        $('#myForm')[0].reset();
                        if(response.type=='add'){
                            var type = 'added'
                        }else if(response.type=='update'){
                            var type = 'updated'
                        }

                        swal("Success!", "You delete a Question!", "success");                          
                        showGroups();
                    }else{
                        alert('Error');
                    }
                },
                error: function(){
                    alert('Could not add data');
                }
            });

    });

我的删除

    //delete- 
    $('#showdata').on('click', '.item-delete', function(){
        var id = $(this).attr('data');
        $('#deleteModal').modal('show');
        //prevent previous handler - unbind()
        $('#btnDelete').unbind().click(function(){
            $.ajax({
                type: 'ajax',
                method: 'get',
                async: false,
                url: '<?php echo base_url() ?>groups/deleteGroup',
                data:{id:id},
                dataType: 'json',
                success: function(response){
                    if(response.success){
                        $('#deleteModal').modal('hide');
                        swal("Success!", "You delete a Group!", "success"); 
                        showGroups();
                    }else{
                        alert('Error');
                    }
                },
                error: function(){
                    alert('Error deleting');
                }
            });
        });
    });

控制器

public function showGroups(){
    $result = $this->group_model->showGroups();
    echo json_encode($result);
}

最后是模型

public function editGroup(){
    $id = $this->input->get('id');
    $this->db->where('id', $id);
    $query = $this->db->get('groups');
    if($query->num_rows() > 0){
        return $query->row();
    }else{
        return false;
    }
}

public function updateGroup(){
    $id = $this->input->post('txtId');
    $field = array(
    'group_name'=>$this->input->post('group')
    );
    $this->db->where('id', $this->user_id);
    $this->db->update('groups', $field);
    if($this->db->affected_rows() > 0){
        return true;
    }else{
        return false;
    }
}

function deleteGroup(){
    $id = $this->input->get('id');
    $this->db->where('id', $id);
    $this->db->delete('groups');
    if($this->db->affected_rows() > 0){
        return true;
    }else{
        return false;
    }
}

public function editGroup(){
    $result = $this->group_model->editGroup();
    echo json_encode($result);
}

public function updateGroup(){
    $result = $this->group_model->updateGroup();
    $msg['success'] = false;
    $msg['type'] = 'update';
    if($result){
        $msg['success'] = true;
    }
    echo json_encode($msg);
}

public function deleteGroup(){
    $result = $this->group_model->deleteGroup();
    $msg['success'] = false;
    if($result){
        $msg['success'] = true;
    }
    echo json_encode($msg);
}

【问题讨论】:

    标签: php mysql ajax codeigniter codeigniter-3


    【解决方案1】:

    不要为 Ajax 进程重新加载页面,您可以使用 id 删除特定的 div 按如下方式更新您的 javascript

    // Show Groups
            function showGroups(){
                $.ajax({
                    type: 'ajax',
                    url: '<?php echo base_url() ?>groups/showGroups',
                    async: false,
                    dataType: 'json',
                    success: function(data){
                        var html = '';
                        var i;
                        for(i=0; i<data.length; i++){
                            html +='<a class="text-decoration-none" href="<?php echo base_url() ?>groups/view_class/'+data[i].id+'" id="group_'+data[i].id+'">'+ // Here i added id to remove using  unique id 
                                        '<div class="col-md-4">'+
                                            '<div class="card" style="width: 15rem; text-align:center;display:inline-block;">'+
                                            '<div class="card-body">'+
                                                '<input type="hidden" name="groupId" value="1" />'+
                                                '<h5 class="card-title">'+data[i].group_name+'</h5>'+
                                                '<h6 class="card-subtitle mb-2 text-muted">Modify your Group</h6>'+
                                                '<a href="javascript:;" class="btn btn-info item-edit" data="'+data[i].id+'"><span class="iconify" data-icon="feather:edit" data-inline="false"></span></a>&nbsp;'+
                                                '<a href="javascript:;" class="btn btn-primary item-delete" data="'+data[i].id+'"><span class="iconify" data-icon="dashicons:remove" data-inline="false"></span></a>'+
                                            '</div>'+
                                            '</div><hr>'+
                                        '</div><hr>'+
                                    '</a>';
                        }
                        $('#showdata').html(html);
                    },
                    error: function(){
                        alert('Could not get Data from Database');
                    }
                });
            }
    

    在您的模型和控制器中返回帖子 ID

        function deleteGroup(){
            $id = $this->input->get('id');
            $this->db->where('id', $id);
            $this->db->delete('groups');
            if($this->db->affected_rows() > 0){
                return $id;
            }else{
                return false;
            }
        }
    public function deleteGroup(){
        $result = $this->group_model->deleteGroup();
        $msg['success'] = false;
        if($result){
            $msg['success'] = $result;
        }
        echo json_encode($msg);
    }
    

    在您的删除 javascript 响应中,使用响应 id 删除特定的 div

    //delete- 
     $('#showdata').on('click', '.item-delete', function(){
        var id = $(this).attr('data');
        $('#deleteModal').modal('show');
        //prevent previous handler - unbind()
        $('#btnDelete').unbind().click(function(){
            $.ajax({
                type: 'ajax',
                method: 'get',
                async: false,
                url: '<?php echo base_url() ?>groups/deleteGroup',
                data:{id:id},
                dataType: 'json',
                success: function(response){
                    if(response.success){
                        var id = response.success;
                        $('#group_'+id).remove(); // Here i remove that part 
                        $('#deleteModal').modal('hide');
                        swal("Success!", "You delete a Group!", "success"); 
                        showGroups();
                    }else{
                        alert('Error');
                    }
                },
                error: function(){
                    alert('Error deleting');
                }
            });
        });
    });
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多