user3676560
user3676560

Reputation: 151

Error and Success Handling in JQuery Ajax PHP

how can i handle error and success messages in Ajax? I have an ajax call where my datas are saved into a db. My php script first checked if a user have x amount. if he has less then x then he should fire a alert (amount < x) Else insert into db.

my php file:

 ...
 .....
 if ($wallet_amount < "100") {
     $wa1 = 0;
     echo $wa1;
 } else {
     $inito = $connection->prepare("INSERT INTO bs (title, start, end, userid, typ, code) VALUES (:title, :start, :end, :pid, :col, :code)");
     $inito->bindValue(':pid', $pid, PDO::PARAM_INT);
     $inito->bindValue(':title', $title, PDO::PARAM_STR);
     $inito->bindValue(':start', $start, PDO::PARAM_STR);
     $inito->bindValue(':end', $end, PDO::PARAM_STR);
     $inito->bindValue(':col', $col, PDO::PARAM_STR);
     $inito->bindValue(':code', $code, PDO::PARAM_INT);
     $inito->execute();

     exit();        
} 

My js file:

$.ajax({
    url: 'add.php',
    data: {
        'title': $('#Name').val(), 
        'start': start, 
        'end': $('#End').val(), 
        'code': $('input[name="code"]:checked').val()
    },
    type: "POST",
    error: function () {
        alert('There was an error while adding events.');
    }
});

My first try goes wrong. I write something like that:

 success: function (response) {
            if (response === 0) {
                alert("Amount < X!");
            } else if (response === 1) {
                alert("Amount > X);
            }
        },
        error: function () {
            alert('There was an error while adding events.');
        }

Upvotes: 0

Views: 77

Answers (3)

Aykut &#199;evik
Aykut &#199;evik

Reputation: 2088

I would not use the content of your respone to switch your error and success cases. Think about different status codes like status code 200 or 204 on success and status code 400 on error for example. Than, you can also code a more type-safe application with explicit return values:

$.ajax({
  statusCode: {
    404: function() {
      alert( "page not found" );
    }
  }
});

Upvotes: 1

rst
rst

Reputation: 2714

To make the code a little more cleaner, I would suggest you start by returning the values with json (even if it is only one variable)

So the PHP look like

php

/* ... */
echo json_encode(array('wal' => $wal));
/* ... */

Next, in your ajax code, you need to read it correctly. E.g.

ajax

success: function(response) {
    var wal = response.wal
    if (wal == 0) {
        alert("Amount < X!");
    } else if (wal == 1) {
        alert("Amount > X);
    } else {
        // ...
    }
}

Try that, and as Jai said, only check for the value, and not for the type (x == 0 instead of x === 0)

Upvotes: 1

Jai
Jai

Reputation: 74738

I guess in success block you have used a strict check ===, this checks the type and value both:

success: function (response) {
        if (response === '0') { // update this 
            alert("Amount < X!");
        } else if (response === '1') {
            alert("Amount > X"); //<---quote is missing i guess it's just a typo.
        }
    },
    error: function () {
        alert('There was an error while adding events.');
    }

Also i am not sure if this check } else if (response === '1') { would ever happen because i don't see if you get '1' in the response.

Upvotes: 1

Related Questions