gpt4 book ai didi

php - 删除操作在 ajax 中不起作用

转载 作者:行者123 更新时间:2023-11-29 07:37:10 25 4
gpt4 key购买 nike

我试图在我的 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代码

检索.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);

删除.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');
}
}

最佳答案

问题出在你的DELETE sql上,

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

应该是

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

此外,我建议使用 PreparedStatement相反,以避免任何 SQL 注入(inject)和语法错误。

关于php - 删除操作在 ajax 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com