PHP代码
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get members");
$data = array();
while($row = mysqli_fetch_array($query)) { // preparing an array
$schoolID = $row["schoolID"];
$schoolname = $row["schoolname"];
$program = $row["program"];
$nestedData = array();
$nestedData[] = $row["schoolname"];
$nestedData[] = $row["level"];
$nestedData[] = $row["program"];
$nestedData[] = $row["startdate"];
$nestedData[] = $row["duration"];
$nestedData[] = " <a href='upDateSchool.php?schoolID=$schoolID' class='btn btn-warning fa fa-pencil'>
</a>
|
<a class='btn btn-danger' id='delete_school' data-id='<?php echo schoolID=$schoolID$schoolID; ?>', href='javascript:void(0)'><i class='glyphicon glyphicon-trash'></i></a>
";
的JavaScript
<script>
$(document).ready(function(){
$(document).on('click', '#delete_school', function(e){
var schoolID = $(this).data('id');
SwalDelete(schoolID);
e.preventDefault();
});
});
function SwalDelete(schoolID){
swal({
title: 'Are you sure?',
text: "Record Deleted is Irreversible!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#3085d6',
cancelButtonText: "No!",
confirmButtonText: 'Yes, delete it!',
showLoaderOnConfirm: true,
preConfirm: function() {
return new Promise(function(resolve) {
$.ajax({
url: 'deleteSchool.php',
type: 'post',
data: 'delete='+schoolID,
dataType: 'json'
})
.done(function(response){
swal('Deleted!', response.message, response.status);
readProducts();
})
.fail(function(){
swal('Oops...', 'Something went wrong with ajax !', 'error');
});
});
},
allowOutsideClick: false
});
}
</script>
用于删除的PHP代码
<?php
header('Content-type: application/json; charset=UTF-8');
$response = array();
$sid = $_POST['delete'];
$query = "DELETE FROM school where schoolID= :schoolID";
$stmt = $db->prepare( $query );
$stmt->execute(array(':schoolID'=>$sid));
if ($stmt) {
$response['status'] = 'success';
$response['message'] = 'School Deleted Successfully ...';
} else {
$response['status'] = 'error';
$response['message'] = 'Unable to delete School ...';
}
echo json_encode($response);
当我单击删除按钮时,它会显示响应成功,但记录不会被删除。
答案 0 :(得分:0)
您的代码包含许多逻辑和语法问题,但仍然存在
问题是,您确实没有在data-id
属性中打印出ID
试着改变这个:
$nestedData[] = " <a href='upDateSchool.php?schoolID=$schoolID' class='btn btn-warning fa fa-pencil'>
</a>
|
<a class='btn btn-danger' id='delete_school' data-id='<?php echo schoolID=$schoolID$schoolID; ?>', href='javascript:void(0)'><i class='glyphicon glyphicon-trash'></i></a>
";
对此:
$nestedData[] = " <a href='upDateSchool.php?schoolID=".$schoolID."' class='btn btn-warning fa fa-pencil'>
</a>
|
<a class='btn btn-danger' id='delete_school' data-id='".$schoolID."', href='javascript:void(0)'><i class='glyphicon glyphicon-trash'></i></a>
";
这可能会有所帮助,但我建议您对代码进行重构。