as I print a custom message from a variable that I have my php file in ajax

-1

This is my AJAX ..

<!-- CREATE AJAX -->
    <script type="text/javascript">
        $(document).ready(function(){
            $('#create').click(function(event){
                event.preventDefault();
                $.ajax({
                    url: "usuario_registrado.php",
                    method: "post",
                    dataType: "text",
                    data:$('form').serialize(),
                    success: function(resultado){
                        if($error_enviado == true){ 
                               $('#mensaje').html("<p>"+resultado+"</p>")
                         }else{
                             $('mensaje').html("<p>"+resultado+"</p>")
                        }
                    } 
                });
            })
        });
    </script>

and this my php ..

<?php 
include("includes/motor.php");
$users = new Usuarios();

if ($_POST) {

        $nombre = $_POST['nombre'];
        $apellido = $_POST['apellido'];
        $email = $_POST['email'];
        $nacimiento = $_POST['fecha'];
        $telefono = $_POST['telefono'];
        $usuario = $_POST['user'];
        $privilegio = $_POST['privilegio'];
        $clave1 = $_POST['clave1'];
        $clave2 = $_POST['clave2'];
        $error_enviado="";
        $error_pass ="";

        if($nombre == "" || $apellido == "" || $email == "" || $nacimiento == "" || $telefono == "" || $usuario == "" || $privilegio == "" || $clave1 == "" || $clave == ""){

             echo "<div class='alert alert-danger text-center' role='alert' style='padding:10px;'>¡Debes rellenar todos los campos!</div>";

?>

I want that if the condition is fulfilled, print me a message, but, do another .. but all using my ajax

    
asked by ebrain machado 13.04.2018 в 23:28
source

2 answers

0

I usually create an array in the back and return it to the front converted to JSON. I always call this array $data and inside the floor I put as keys success and message .

Success I use to know if the operation I want to perform was successful or not. While message returns the personalized message depending on the action.

I will try to guide you a little with the information you have given, and the way I am mentioning it to you.

PHP

<?php 
    include("includes/motor.php");
    $users = new Usuarios();

    if ($_POST) {
       $nombre = $_POST['nombre'];
       $apellido = $_POST['apellido'];
       $email = $_POST['email'];
       $nacimiento = $_POST['fecha'];
       $telefono = $_POST['telefono'];
       $usuario = $_POST['user'];
       $privilegio = $_POST['privilegio'];
       $clave1 = $_POST['clave1'];
       $clave2 = $_POST['clave2'];
       $error_enviado="";
       $error_pass ="";

       if($nombre == "" || $apellido == "" || $email == "" || $nacimiento == "" || $telefono == "" || $usuario == "" || $privilegio == "" || $clave1 == "" || $clave == ""){
          $data['success'] = false;
          $data['message'] = "Tienes que llenar todos los campos.";
       } else {
          $data['success'] = true;
          $data['message'] = "Todos tus campos están llenos :)";
       }

     return json_encode($data);
?>

JS

$(document).ready(function(){
    $('#create').click(function(event){
       event.preventDefault();
       $.ajax({
         url: "usuario_registrado.php",
         method: "post",
         dataType: "text",
         data:$('form').serialize(),
         success: function(data){
            console.log(data) //Checa tu consola del navegador.
            if(data.success){
              $('#message').html("<p class='alert alert-success text-center'>"+data.message+"</p>");
             } else {
              $('#message').html("<p class='alert alert-danger text-center'>"+data.message+"</p>");
             }
         } 
      });
 });
    
answered by 14.04.2018 / 00:30
source
0

If you want personalized messages install this library here is the link to the page link

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
success: function(resultado){                                          
swal("Good job!", "You clicked the button!", "success");
}    
</script>
    
answered by 13.04.2018 в 23:55