duanjupiao4730 2018-06-28 09:30
浏览 36
已采纳

使用AJAX和PHP删除用户记录时出现问题

Scenario:

I have used AJAX to delete the record and PHP as the programming language. The AJAX part is given below:

Here $('#id') is the id of the button which contains the value of user's id from the database dynamically with the help of this part: id='.$d['ID'].' implemented in the front end.

<script>
    function deleteRecord() {
        var var1 = $('#id').html();    
        var result = confirm("Do you want to delete this Record?");
        if(result){
            //call ajax
             $.ajax({
                 url: '<?php echo base_url();?>index.php/Device/delete', 
                 type: 'POST',
                 data: {
                 id: var1
                 },     
                 success: function(result){
                 $("#id").html(result);
                 alert('You have successfully deleted the user.');
                 },
                 error(e){
                 alert('Problem while interacting to the server.');
                 }
             });
            }
        else{
            console.log('Operation Cancelled.');
        }
    }
    </script>

This is the front end part where Button is used to delete the records. I have used id='.$d['ID'].' to get the value of the id dynamically from the database.

<td> <button class="btn" type="submit" onclick="deleteRecord()" id='.$d['ID'].'>Delete 
</button> </td>

This is the url as mentioned in the AJAX:

url: '<?php echo base_url();?>index.php/Device/delete'

In the above URL, Device is the name of the Controller and delete is the method of the controller which is given below:

public function delete($id = '') {
    $login = $_SESSION['username'];
    if (! in_array($login, $this->main_model->get_authorized_users())) {
        $redirect = site_url('device');
        redirect($redirect, 'refresh');
    }
    if ($this->input->post('submit')) {
        $username = $this->input->post('username');
        $deviceid = $this->input->post('deviceid');
        if ($deviceid == null || $username == null || $login == null) {
            $redirect = site_url('device');
            redirect($redirect, 'refresh');
            return;
        }
    } else {
        if ($id == '') {
            $redirect = site_url('device');
            redirect($redirect, 'refresh');
        }
        $res = $this->device_model->delete();
        $data['action'] = 'delete';
        $data['id'] = $id;
        $data['result'] = $this->device_model->get_device($id);
        $data['body'] = $this->load->view('register_view', $data, true);
        $this->load->view('template', $data);
    }
}

Here is the Model part:

function delete(){
    $username = $this->input->get('username');
    $deviceid = $this->input->get('deviceid');
    $id = $this->input->get('id');
    $language= $this->input->get('language');
    $deleted_by=$_SESSION['username'];
    $sql = "DELETE FROM devices
    WHERE id = $id";
    $query = $this->db->query ( $sql );
    //log update
    $this->log_device_delete($id,$username,$deviceid,$deleted_by);
}

Current Output:

  1. When DELETE button is pressed then Confirmation Box is displayed.
  2. In Confirmation Box, "Do you want to delete this Record?" is asked.

Issue Faced:

When user presses the OK button then the message called "You have successfully deleted the user." is displayed but the record is not deleted.

Required Solution:

When the user presses the OK button on the Confirmation Box then the user record should be deleted.

What correction do I need here?

Suggestions are highly appreciated.

  • 写回答

3条回答 默认 最新

  • dqmgjp5930 2018-06-28 09:35
    关注

    You are using $d['ID'] in id attribute. But it need to be placed as value in some field.

    The code that you need to update is:

    <td> 
        <button class="btn" type="submit" onclick="deleteRecord(this)" data-id="<?php echo $d['ID']; ?>">Delete</button>
    </td>
    

    And update in JS as:

    function deleteRecord(e) {
        var var1 = $(e).data("id"); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题