Sending ajax with notification

0

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;
  }
?>
    
asked by anonymous 28.11.2016 / 13:46

1 answer

2

PHP has no return, that's the problem. In ajax, on the line:

success:function(data){

"date" will be what you have returned from PHP. For example, let's say you want to know if the query was entered successfully. You can put the code inside a try / catch block. It could look like this:

PHP:

<?php 
  $nome = $_POST['nome_usuario'];
  $sobrenome = $_POST['sobrenome_usuario'];

    $mysqli = new mysqli('localhost','root','@0202','ajax');
    $sql = "INSERT INTO 'usuario' ('idusuario','nomeusuario','sobreusuario') VALUES (NULL,'{$nome}','{$sobrenome}')";

    if($mysqli->query($sql) === false){
      echo mysqli_connect_error();
    }
    else{
    echo "salvo";
    }
?>

Details about mysqli_connect_error () here.

And in ajax, within the success function, you do something like:

success:function(data){
  if(data == "salvo"){
    //código em caso de sucesso
  }
  else{
    alert(data);
    //ou talvez
    alert("erro ao salvar");
  }
}
    
28.11.2016 / 14:07