PHP code
$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 code for deleting
<?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);
When I click the delete button, it shows response success but the record does is not deleted.
Your code contains many logical and syntax problems, but still.
The problem is, that you exactly don't print out ID in data-id
attribute.
Try to change this:
$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>
";
To this:
$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>
";
It may help, but I recommend you to perform refactoring of your code.