I have some server side code that, upon the checkout being fulfilled returns either a true or a false. Through the console log it looks like this {"success": true, "message": "Added to db"}
. How can I correctly write in the ajax success condition that if the answer is true, then we do something, otherwise we do something else?
handler php
echo json_encode(['success' => true, 'message' => 'Added to db']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Wrong captcha']);
}
index.php
$(document).ready(function() {
$('#submit').click(function() {
var username = $('#username').val();
var email = $('#email').val();
var message = $('#message').val();
var captcha = $('#captcha').val();
if(username === ''){
alert('Please input data in all fields');
} else {
$.ajax({
type: "POST",
cache: false,
url: '/data/insert.php',
data: {username: username, email: email, message: message, captcha: captcha},
success: function(response) {
console.log(response);
}
});
}
});
});