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.');
}