I would like to use a return variable from my PHP file to display a custom message of possible errors in SweetAlert. It is possible?
File PHP:
if ($a > $b) {
echo "true";
$msg = "First personalised message";
exit;
} elseif ($a == $b) {
echo "true";
$msg = "Second personalised message";
exit;
} else {
echo "false";
exit;
}
JAVASCRIPT CODE:
$(document).ready(function ($) {
$('#moving_send_toners').on('submit',function(e) {
if ($("#moving_send_toners").valid()) {
$.ajax({
url:'action_send_toners.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
if (data == 'false') {
console.log(data);
swal('Sucesso!', 'Ação realizada.', 'success');
$('#moving_send_toners')[0].reset();
}
if (data == 'true') {
swal('Ops!', "Toner não pode ser enviado :(", "error");
}
},
error:function(data){
swal("ERRO!", data.msg, "error");
}
});
}
e.preventDefault();
})
});