Good morning, I'm having a problem in a test, I'm returning a notification, but the notification of only one message, that is, a success message, I can not get an error message, type an if else , and I wonder if it's not a gambiarra what I did, I do not understand much of ajax and jquery, but it's working kkk, I wanted to know if it was the best option! because I will follow this code to insert, update and delete!
<form id="cadastrarUsuario" action="" method="POST" class="form-group">
Nome: <br>
<input required type="text" name ="nome_usuario"><br>
Sobrenome: <br>
<input required type="text" name ="sobrenome_usuario"><br>
<hr>
<button type="submit">Enviar</button>
</form>
<script src="js/classie.js"></script>
<script src="js/notificationFx.js"></script>
<script type="text/javascript">
$(function(){
$('#cadastrarUsuario').submit(function(event){
event.preventDefault();
var formDados = new FormData($(this)[0]);
$.ajax({
url:'addUsuario.php',
type:'POST',
data:formDados,
cache:false,
contentType:false,
processData:false,
success:function(data){
if(data == "salvo"){
$('#cadastrarUsuario').trigger("reset");
var notification = new NotificationFx({
wrapper : document.body,
message : '<p>Deu Certo</p>',
layout : 'growl',
effect : 'scale',
type : 'notice', // notice, warning, error or success
ttl : 6000,
onClose : function() { return false; },
onOpen : function() { return false; }
});
notification.show();
}
else{
$('#cadastrarUsuario').trigger("reset");
var notification = new NotificationFx({
wrapper : document.body,
message : '<p>Deu Erro</p>',
layout : 'growl',
effect : 'scale',
type : 'notice', // notice, warning, error or success
ttl : 6000,
onClose : function() { return false; },
onOpen : function() { return false; }
});
notification.show();
}
},
dataType:'html'
});
return false;
});
});
</script>
Php
<?php
$nome = $_POST['nome_usuario'];
$sobrenome = $_POST['sobrenome_usuario'];
try{
$mysqli = new mysqli('localhost','root','@0202','ajax');
$sql = "INSERT INTO 'usuario' ('idusuario','nomeusuario','sobreusuario') VALUES (NULL,'{$nome}','{$sobrenome}')";
$mysqli->query($sql);
echo "salvo";
}
catch(Exception $e){
$retorno = "Erro ao salvar. ".$e->getMessage();
echo $retorno;
}
?>