Show gif loading php

0

I need to put a loading message while a sql statement is executed, since the specific statement will take a while to complete and I would like a gif or a simple message to be displayed. I attached my PHP code

try{
    include ('./pdo.php');

    $consulta = $con->prepare("insert into datos values(:codigo,:anio,:mes,:precip,:temp,:tempMax,:tempMin)"); 
    $consulta->bindValue(':codigo',$codigo,PDO::PARAM_STR);
    $consulta->bindValue(':anio',$anio,PDO::PARAM_STR);
    $consulta->bindValue(':mes',$mes,PDO::PARAM_STR);
    $consulta->bindValue(':precip',$precip,PDO::PARAM_STR);
    $consulta->bindValue(':temp',$temp,PDO::PARAM_STR);
    $consulta->bindValue(':tempMax',$tempMax,PDO::PARAM_STR);
    $consulta->bindValue(':tempMin',$tempMin,PDO::PARAM_STR);
    $resultado = $consulta->execute();  


}catch(PDOException $e){
    echo 'Error:'.$e->getMessage()."\n";
    echo "<br>";
    echo 'Error getFile:'.$e->getFile()."\n";
}

This code is inside a loop and if there are many records that you have to insert it takes a while. Thank you in advance to everyone!

    
asked by Adrian 15.10.2017 в 18:54
source

1 answer

1

The quickest and simplest thing you can do, is to use an Ajax in the HTML that shows a GIF while the asynchronous request has not finished, and when the "success" condition is generated, show the content as such. Since PHP only executes server-side code, not client-side code (just like JavaScript does).

I'll give you an example.

HTML code + Ajax (JQuery):

<div class="contenido"></div>

<script>
    var url = "archivo.php";
    var data = { variable : "valor" };
    $.ajax({
        url: url,
        data: data,
        beforeSend: function() {
            $(".contenido").html("<img src='img/imagen.gif'>");
        },
        success: function(r) {
            $(".contenido").html(r);
        }
    });
</script>

Obviously, it's an example to the air what I give you. You modify it to your taste and need. I hope you find it useful.

    
answered by 15.10.2017 / 19:22
source