load in css and jquery

Asked

Viewed 508 times

1

I’m trying to make a system of load to inform the user that the page is being loaded.

I have a php page that mounts a very large report, and it takes around 10 seconds to display. On how much this page turns blank. What I want is to put a load until the page is loaded.

I tried to do so, at the beginning of php I put this:

<div class="se-pre-con"><p>PROCESSANDO DADOS</p></div>

I put before the tag

.se-pre-con {
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background: url(../Img/Loading.gif) center no-repeat #FFFFFF;
}
.se-pre-con p {
    text-align: center;
    height: 15px;
    position: absolute;
    top: 0;
    bottom: -125px;
    left: 0;
    right: 0;
    margin: auto;
}

No jquery looks like this:

$(window).on("load", function () {
    $(".se-pre-con").fadeOut();
});

The problem is that the load even appears, but it appears after about 5 seconds. How do I make it appear and then php starts loading the data? Is there a way?

------------ EDIT

Follow my php page, so you can tell me if I’m doing something wrong.

<!-- Loading -->
<div class="se-pre-con"><p>PROCESSANDO DADOS</p></div>

<?php
// AQUI ESTOU CONECTANDO COM O BANCO DE DADOS E MONTANDO UM ARRAY COM O RESULTADO
?>


<!-- Relatório -->
<table class="relatorio">

    <tr>
        <td>ID</td>
        <td>PRODUTO</td>
        <td>QUANTIDADE</td>
        <td>VALOR</td>
    </tr>


    <?php
    // Aqui monto o relatório com o array
    foreach ($curva as $c) {
        ?>
        <tr>
            <td><?= $c['id'] ?></td>
            <td><?= $c['nome'] ?></td>
            <td><?= $c['qtd'] ?></td>
            <td><?= $c['valor'] ?></td>
        </tr>
        <?php
    }
    ?>
</table>

  • Already tried: $(Document). ready(Function () { }); ??

  • 1

    Maybe the problem is the size of the gif, these 5 seconds can be the time it takes for the browser to load the gif, instead said try to put a <H1> just to test if it is rendered as soon as it loads the page

  • Is using ajax?

  • @I’m not using ajax

  • @Lukssys already tried to remove the image, and the problem is not this.

  • @Aline did not understand very well

  • @Hugoborges, how many records are returned in this query to the bank? I see no reason for your page to delay, so the query may be the problem.

  • @jlHertel the question not and the amount of registration, are several factors. I summarized the report to post here, but in it I do various calculations and checks in the database. This is a report with a lot of data so it takes a while.

  • @Hugoborges, then it is suggested to load the blank page and bring the data later via AJAX. The only problem is that if it’s as much data as you said yourself, AJAX won’t be the solution. A third (uglier) option would be to load the report into an iframe, to allow the external page to be free to display the loading div you want.

  • I don’t think iframe would be cool, I think AJAX would be the best solution. I liked your idea in AJAX, I just don’t know how to use it in my code. I don’t understand how I’m going to load the data in the table. Could you give a more detailed example?

Show 5 more comments

2 answers

3


You better make a page that only carries the div:

<!-- Loading -->
<div class="se-pre-con"><p>PROCESSANDO DADOS</p></div>

And another page that only loads the content of the report

<tr>
    <td>ID</td>
    <td>PRODUTO</td>
    <td>QUANTIDADE</td>
    <td>VALOR</td>
</tr>


<?php
// Aqui monto o relatório com o array
foreach ($curva as $c) {
    ?>
    <tr>
        <td><?= $c['id'] ?></td>
        <td><?= $c['nome'] ?></td>
        <td><?= $c['qtd'] ?></td>
        <td><?= $c['valor'] ?></td>
    </tr>
    <?php
}
?>

Then I’d be like this:

http://www.meusite.com.br/relatorio -> Just answer the div

http://www.meusite.com.br/relatorio-corpo -> Answers the body of the report.

And with ajax you carry the content:

$.ajax({
    'url': 'http://www.meusite.com.br/relatorio-corpo',
    'success': function(responseHtml) {
         $(".se-pre-con").html(responseHtml);
     }
 });

----- EDIT

Remembering that this is not the best approach, since it will be moving HTML data across the network. The best would be to answer the data in JSON and mount the HTML using Javascript

  • Cool, I’m trying to make it work based on your idea. Well I already have the data in JSON as I would for javascript to fill out the report ?

  • You could do something like:

  • I managed to do it your way here. It worked. But I could put an example of how to do with JSON

  • You could do something like: $.ajax({ 'url': 'http://www.meusite.com.br/reportario-json', 'Success': Function(responseJson) { $.each(responseJson, Function(key, value)) { "<tr><td>" + value.id + "</td></tr>"; } } &#Xa>; });

  • You’re better off here: https://jsfiddle.net/o39fgvk4/

  • What is the problem of moving HTML over the network? The site uses SSL has some problem with, security?

  • It is more a matter of internet bandwidth usage and performance. Depending on the number of request it is better to decrease traffic. I detailed more here: http://blog.toneladas.com.br/respondendo-no-stackoverflow-1.html

Show 2 more comments

1

Your problem is that PHP is still sending the answer to the browser, and for this reason, the page has not finished loading.

A more interesting strategy is to open your empty report, show the div and only then load the PHP data using an AJAX call.

A very simple example would be:

$(document).ready(function(){
  $(".se-pre-con").fadeIn();
  $.ajax({
    url: 'meu php',
    success: function(response) {
      /* Monte aqui o seu HTML */
      /* Ao final, remova a div novamente: */
      $(".se-pre-con").fadeOut();
    }, 
    error: function(error) {
      /* Trate os erros */
    }
  });
});
  • Will I mount the html inside Javascript? I don’t think it would work. For ease I’ll edit the question with my php page. If I can look and explain myself better.

  • @Hugoborges, as the php script will take time to respond, I see two options: sit and wait to load, or do this all via AJAX as I suggested. You don’t need to necessarily assemble HTML by Javascript, you can do this all in php and just take this result and insert it back into the DOM.

  • I get it, but I don’t know how to do it in my current structure, could it help me? I put how I’m putting together the report.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.