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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路