Dev.W
Dev.W

Reputation: 2370

Ajax return error to call error ajax error call

I have an ajax function which updates my database.. The function works perfectly well and after updating the the database I call the successAlert() function I have created.. however now I want to call the error function in case of error however on testing purposely to break code I still get the successAlert().

Ajax / Javascript:

var share = "test"
var custid = "test"    

$.ajax({
            url: "assets/ajax/customer-rec.php",
            type: "POST",
            data: {UpdateAccount: "yes",custid: custid,share: share},
            success: function(result){
                successAlert()      
            },
            error: function(result){
                errorAlert()    
            }
        });

PHP to update Database

if (isset($_POST['UpdateAccount'])){

    $custid = $_POST['custid'];
    $share = $_POST['share'];
    $query="UPDATE `users` SET `share_ord`='$share' WHERE id= $custid";
    $stmt = mysql_query($query);

    if($stmt === false){
        return false
    }

}

Upvotes: 0

Views: 93

Answers (4)

Jitendra Kumar. Balla
Jitendra Kumar. Balla

Reputation: 1213

.ajax() will call on success method because, once your request is processed successfully by the server then it reruns HTTP_OK to the client and if .ajax not received HTTP_OK, then it will call error. According to your code, it will call success, because url is exists and server will send HTTP_OK to the browser. If you want to generate error: then give wrong url or disconnect internet or simply change

In PHP:

if($stmt === false){
      //If you want really generate some http error.
       header('X-PHP-Response-Code: 500', true, 500);
       exit(0);
      //or else continue as per code
      // return false;   
    }

In your JS:

$.ajax({
        url: "assets/ajax/customer-rec.php",
        type: "POST",
        data: {UpdateAccount: "yes",custid: custid,share: share},
        success: function(result){
            if(!result){
               showErrorAlert()
            } else  {
               showSuccessAlert()
            }      
        },
        error: function(result){
            showErrorAlert()    
        }
    });  

Upvotes: 0

Alex Andrei
Alex Andrei

Reputation: 7283

The error callback is fired when the server returns a HTTP status code that indicates an error, as such you should send one, ex HTTP 500

if($stmt === false){
    header('HTTP/1.1 500 Server error');
}

See here a list of HTTP status codes

Upvotes: 0

Ali Raza
Ali Raza

Reputation: 147

To get error you need to return the status code '404' from the php function which is serving your request.

Upvotes: 0

Ashish Choudhary
Ashish Choudhary

Reputation: 2034

return false is not an error. If you want to send the error use headers like

header('X-PHP-Response-Code: 404', true, 404);

you can call the same errorAlert() function in success also so that

$.ajax({
        url: "assets/ajax/customer-rec.php",
        type: "POST",
        data: {UpdateAccount: "yes",custid: custid,share: share},
        success: function(result){
            if(result === false){
               errorAlert()
            } else  {
               successAlert()
            }      
        },
        error: function(result){
            errorAlert()    
        }
    });

Upvotes: 5

Related Questions