Receive Jquery and PHP response from second to second

Asked

Viewed 332 times

0

Galley,

I have a page in PHP that makes a query in the database at the end of the processing I show the amount of records updated. This process takes a while, in my Jquery I only show the total of affected records, how could I change my Jquery to show time to time, in case every 1 second it shows the user the amount of records.

EDIT: ATTENTION I can not keep calling the PHP page several times, I need to call a single time and listen to her or receiving data from her!

var form_data = new FormData();

         form_data.append('id', idSel);
         $.ajax({
             url: 'postar_reenvio.php', // caminho para o script que vai processar os dados
             type: 'POST',
             data: form_data,
             cache: false,
             contentType: false,
             processData: false,
             beforeSend: function () {
                $('.resp').html("<img src='images/spin.gif' alt='Notificação' height='70' width='70'/> <br>Enviando Mensagens, aguarde!");
            },
             success: function(response) {
  • Creates a function that makes this control. And success use the setTimeout to set the time to call this function again

  • In my PHP page I only use echo($cont); I wanted to put an echo inside the Loop I have on the PHP page and every 1 second and reply to Jquery

  • @abfurlan It is not equal pq the question that you posted it keeps calling the function "site" I do not want to call the function again, I want to get the data from the PHP page.

3 answers

1

0

I believe that in the function itself

beforeSend();

You can create a loop stating the amount of records entered. (If I understood it that way).

  • By clicking on the button I want to keep receiving PHP data with echo($cont); Beforesend serves in the case of the upload image I used in the example.

  • Show, issue resolved then?

0

You can use the window.setInterval JS to call the function passing as parameter the function you want to call and the time in milliseconds.

The Code would look like this:

var intervalo = window.setInterval(consulta, 1000);
function consulta() {
   var form_data = new FormData();
     form_data.append('id', idSel);
     $.ajax({
         url: 'postar_reenvio.php', // caminho para o script que vai processar os dados
         type: 'POST',
         data: form_data,
         cache: false,
         contentType: false,
         processData: false,
         beforeSend: function () {
            $('.resp').html("<img src='images/spin.gif' alt='Notificação' height='70' width='70'/> <br>Enviando Mensagens, aguarde!");
        },
         success: function(response) {
        }
   }

And if you want to abort the script use clearInterval(intervalo);.

  • In case my call is in the button click $('#submitButton'). click(Function() {

  • Will I need to create a Function and then call on the button? Something else it won’t keep calling my php page several times?

  • Wait there, let me see if I understand. You want to update in real time the values of PHP cont that is returned from AJAX?

  • I want to call the PHP page ok, then inside PHP I have a Loop and inside this Loop I have an echo("total records") every passed in the loop I want to send the amount of updated records.

  • I don’t know if this is possible this way, because PHP will process all the looping and will return all the Excerpts at once to your AJAX. You can try using a session variable by setting the container value, and a setInterval by printing your session variable, or maybe a php server variable

  • So I’m researching about it and I haven’t found anything yet it’s like a Thread listening, but I can only call the php page one time and listen to it sending me the affected records.

Show 1 more comment

Browser other questions tagged

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