删除操作无法在ajax

时间:2018-01-13 14:46:05

标签: php jquery mysql sql sql-delete

我尝试在我的CRUD操作中进行删除操作,但是当我点击删除按钮时似乎没有任何效果:每次单击删除按钮时onclick事件都没有响应。

我已经尝试了一切,但似乎没有任何工作。

以下是源代码:

HTML

<!-- remove modal -->
    <div class="modal fade" tabindex="-1" role="dialog" id="removeDeptModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title"><span class="glyphicon glyphicon-trash"></span> Remove Member</h4>
          </div>
          <div class="modal-body">
            <p>Do you really want to delete this department ?</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary" id="removeBtn"><span class="glyphicon glyphicon-trash"></span> Delete</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <!-- /remove modal -->

PHP代码

retrieve.php

<?php 
require_once('../../includes/config.php'); 
$output = array('data' => array());

$sql = "SELECT * FROM department order by id desc";
$query = $mysqli->query($sql);
$x = 1;
while ($row = $query->fetch_assoc()) {
    $actionButton = '
    <a type="button" data-toggle="modal" data-target="#editMemberModal" class="btn btn-info btn-sm" onclick="editMember('.$row['id'].')"> <span class="glyphicon glyphicon-edit"></span></a>
    <a type="button" data-toggle="modal" data-target="#removeDeptModal" class="btn btn-danger btn-sm" onclick="removeMember('.$row['id'].')"> <span class="glyphicon glyphicon-trash"></span></a>';

    $output['data'][] = array(
        $x,
        $row['dept_code'],
        $row['dept_name'],
        $actionButton
    );
    $x++;
}
// database connection close
$mysqli->close();
echo json_encode($output);

remove.php

<?php 
require_once('../../includes/config.php'); 

$output = array('success' => false, 'messages' => array());
$memberId = $_POST['member_id'];

$sql = "DELETE FROM department WHERE id = {$memberId}";
$query = $mysqli->query($sql);
if($query === TRUE) {
    $output['success'] = true;
    $output['messages'] = 'Successfully Deleted';
} else {
    $output['success'] = false;
    $output['messages'] = 'Error while deleting the Department information';
}
// close database connection
$mysqli->close();
echo json_encode($output);
?>

最后,jquery代码:

    function removeMember(id = null) {
        if(id) {
            // click on remove button
            $("#removeBtn").unbind('click').bind('click', function() {
                $.ajax({
                    url: 'dept/remove.php',
                    type: 'post',
                    data: {member_id : id},
                    dataType: 'json',
                    success:function(response) {
                        if(response.success == true) {                      
                            $(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
                                  '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'
'<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+'</div>');
// refresh the table
                            manageDeptTable.ajax.reload(null, false);
                            // close the modal
                            $("#removeDeptModal").modal('hide');
                        } else {
                            $(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
                                  '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+ '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.messages+
                                '</div>');
                        }
                    }
                });
            }); // click remove btn
        } else {
            alert('Error: Refresh the page again');
        }
    }

1 个答案:

答案 0 :(得分:1)

问题在于您的DELETE sql,

$sql = "DELETE FROM department WHERE id = {$memberId}";

应该是

$sql = "DELETE FROM department WHERE id = $memberId";

另外,我建议改用PreparedStatement,以避免任何SQL注入和语法错误。