I'm having some trouble getting the errors from a php script called with Ajax.
I've got an ajax which is directing to a suppression script.
Here's my Js :
$('.js-delete-link').click(function (e) {
e.preventDefault();
let id = $(this).closest('tr').find('.id').text();
if (confirm('Voulez-vous vraiment supprimer le message ?')) {
let href = $(e.currentTarget).attr('href');
$.ajax({
url:href,
type:'post',
success:function(data){
let msgJson = JSON.parse(data);
bootstrapNotify(msgJson.msg,msgJson.type);
},
})
}
});
and here's my php script where i'm actually deleting the items.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_GET['id'])) {
$id = FILTER_INPUT(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id === false) {
$arr = array("msg" => "Id incorrect ou non spécifié", "type" => 'danger');
} else {
if ($messageModelDb->delete($id)) {
$arr = array("msg" => "Le ou les messages ont été supprimés", "type" => 'success');
} else {
$arr = array("msg" => "Le message n'existe pas !", "type" => 'danger');
}
}
}
echo json_encode($arr);
} else {
$_SESSION['error'] = "Vous n'êtes pas autorisé.";
redirect('liste-messages');
}
In my php i try to catch any error which could occur during the query and if it happens i pass a message according to the error.
However, no matter what i always get a success message even if the id does not exist.
Sorry if this may seem totally obvious but i'm new to all this and i'm kind of stuck here!
Thank you for your help !