Doubt sent reply with ajax

0

Help me understand how this works please. How do you receive in .outer_div the message that is created in edit_banner.php through data by request ajax? I see you have this:

$(".outer_div").html(data).fadeIn('slow');

But I do not understand what is going on in the data. Is it sent in the same ajax or is the response created in edit_banner.php?

This would be the ajax script. Understand that there is a div with class .outer_div where this is received.

<script>
    $("#editar_banner").submit(function(e) {

          $.ajax({
              url: "ajax/editar_banner.php",
              type: "POST",
              data: $("#editar_banner").serialize(),
               beforeSend: function(objeto){
                $("#loader").html("Cargando...");
              },
              success:function(data){
                    $(".outer_div").html(data).fadeIn('slow');
                    $("#loader").html("");
                }
        });
         e.preventDefault();
    });
</script>

on the other hand I have the edit_banner.php

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST["titulo"])){
/* Llamar la Cadena de Conexion*/
include ("../../conexion.php");
// escaping, additionally removing everything that could be (html/javascript-) code
 $titulo = mysqli_real_escape_string($con,(strip_tags($_POST['titulo'], ENT_QUOTES)));
 $descripcion = mysqli_real_escape_string($con,($_POST['descripcion']));
 $descripcion_corta = mysqli_real_escape_string($con,($_POST['descripcion_corta']));
 $url_web = mysqli_real_escape_string($con,($_POST['url_web']));
 $orden = intval($_POST['orden']);
 $estado = intval($_POST['estado']);
 $id_banner=intval($_POST['id_banner']);
 $sql="UPDATE portafolio SET titulo='$titulo', descripcion='$descripcion', orden='$orden', estado='$estado', descripcion_corta='$descripcion_corta', url_web='$url_web' WHERE id='$id_banner'";
 $query = mysqli_query($con,$sql);
// if user has been added successfully
if ($query) {
    $messages[] = "Datos  han sido actualizados satisfactoriamente.";
} else {
    $errors []= "Lo siento algo ha salido mal intenta nuevamente.".mysqli_error($con);
}

if (isset($errors)){

        ?>
        <div class="alert alert-danger" role="alert">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
                <strong>Error!</strong>
                <?php
                    foreach ($errors as $error) {
                            echo $error;
                        }
                    ?>
        </div>
        <?php
    }
    if (isset($messages)){

        ?>
        <div class="alert alert-success" role="alert">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                <strong>¡Bien hecho!</strong>
                <?php
                    foreach ($messages as $message) {
                            echo $message;
                        }
                    ?>
        </div>
        <?php
    }

}

    
asked by RicardoKra 06.11.2018 в 21:02
source

2 answers

1

Since your question refers to what you get in the data variable in this fragment:

$(".outer_div").html(data).fadeIn('slow');

I'll answer that.

As you will see by doing an ajax query in which you send data to the server, this data has been specified here:

data: $("#editar_banner").serialize()

That's what you send to the server, in the server you receive and you work with what you have sent, if everything in the server goes well you are building an alert with this code:

if (isset($messages)){
 ?> 
<div class="alert alert-success" role="alert"> 
<button type="button" class="close" data-dismiss="alert">&times;</button> 
<strong>¡Bien hecho!</strong> 
<?php 
foreach ($messages as $message) {
 echo $message; 
} ?>
</div> 

Well then I explain, the ajax queries send data to the server and are able to receive a response from this so we have the success method of ajax. In your code you are receiving the message that you have built on the server and you are showing it on div with this code:

success:function(data){ //recibiendo la alerta que se construyo en el servidor
 $(".outer_div").html(data).fadeIn('slow');// Agregando al div la alerta que se construyo en el servidor
 $("#loader").html("");//vaciando el elemento loader
 }

And as you see it works like this, ajax allows you to send and receive data from the server in that way.

    
answered by 06.11.2018 / 23:50
source
0
data: $("#editar_banner").serialize()

It is only the content of the form that you are serializing it and sending it to edit_banner.php

    
answered by 06.11.2018 в 21:59