Simondebn
Simondebn

Reputation: 23

Handling php errors after ajax call

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 !

Upvotes: 0

Views: 118

Answers (2)

jeroen e
jeroen e

Reputation: 144

if isset($_GET['id']) is false, nothing will happen because you should put the else statement like this:

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_GET['id'])) {

        $id = FILTER_INPUT(INPUT_GET, 'id', FILTER_VALIDATE_INT);
        if (!$id) {
            $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');
            }
        }
    } else {
        $arr = array("msg" => "ID is not set", "type" => 'danger');
    }
} else {
    $arr = array("msg" => "Invalid request method", "type" => 'danger');
}

echo json_encode($arr);

Then in the Javascript file, you can add an error callback:

success: function(){  
    ... 
},
error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert("Status: " + textStatus + " - Error: " + errorThrown); 
} 

Upvotes: 1

AlexandrX
AlexandrX

Reputation: 838

AFAIK, the FILTER_INPUT could return NULL too, so $id === false will be false in this case. Try to use if(!$id) instead, if you sure that your $id could not be 0

Upvotes: 0

Related Questions