jquery how to receive data line by line in a loop

Asked

Viewed 155 times

1

In the test.php file I have:

<?php

echo "<input type='button' value='Envia' class='btenvia'/><br><br>";
echo "<div id='mostradados'></div>";

?>

<script src="jquery.min.js"></script>

   <script type="text/javascript">
    $(document).ready(function(){

        $(".btenvia").click(function(){

            $.post("dados.php", {opcao: 1 },
            function(info){

                $("#mostradados").html(info);

            });
        });
      });
     </script>

In the.php data file I have for example a loop with for:

<?php
for ($i = 0; $i < 100000; $i++) {

    echo "linha ".$i."<br>";
}
?>

How do I get the loop to appear line by line in my view, instead of everything appearing all at once. I was thinking of ajax but I don’t see how to do it.

Console output:

linha 0<br>linha 1<br>linha 2<br>linha 3<br>linha 4<br>linha 5<br>linha 6<br>linha 7<br>linha 8<br>linha 9<br>linha 10<br>linha 11<br>linha 12<br>linha 13<br>linha 14<br>linha 15<br>linha 16<br>linha 17<br>linha 18<br>linha 19<br>linha 20<br>linha 21<br>linha 22<br>linha 23<br>linha 24<br>linha 25<br>linha 26<br>linha 27<br>linha 28<br>linha 29<br>linha 30<br>linha 31<br>linha 32<br>linha 33<br>linha 34<br>linha 35<br>linha 36<br>linha 37<br>linha 38<br>linha 39<br>linha 40<br>linha 41<br>linha 42<br>linha 43<br>linha 44<br>linha 45<br>linha 46<br>linha 47<br>linha 48<br>linha 49<br>linha 50<br>linha 51<br>linha 52<br>linha 53<br>linha 54<br>linha.... 
  • I think it would be better if you returned a json in your php and read it with jquery ajax

  • Before $("#mostradados").html(info); puts a console.log(info); and see what returns in the console. If possible edit your question and include the information of what is returned.

  • In the log console returns all values at once. Wanted to return the value after each row of the loop, not have to wait for the result of all rows.

  • All right. Just for test purposes, could edit your question and put what you saw on the console?

  • Your problem is display (little by little is some kind of visual effect) or performance (it’s slow to pick up all at once)?

1 answer

1

Good you can split the same Breakline in the code below

$(Document). ready(Function(){

    $(".btenvia").click(function(){

        $.post("dados.php", {opcao: 1 },
        function(info){

            var rt = info.split("\n");
            var count = 0;
            while( count <= rt.length){
                console.log(rt[count]);
                count++;
            }
        });
    });
  });

But recommend you return a json and read it with much more effective jquery ajax

Browser other questions tagged

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