Javascript PHP returns

Asked

Viewed 120 times

1

This one and HTML

<div class="widget widget-table action-table">
  <div class="widget-header"> <i class="icon-check"></i>

  </div>

  <div class="widget-content">
    <table class="table table-striped table-bordered">

      <tbody>
        <br/>
        <center>
          <textarea  cols="150" rows="7" class="input-xxlarge" type="text" id="login" name="login" placeholder="Sua lista"></textarea> <br /><br />

          <button class="btn btn-mini btn btn-success" id="submitValidacao"  type="submit">Iniciar teste</button>
          <button class="btn btn-mini btn btn-danger" type="button">Cancelar e limpar</button>
        </center>

    <hr>
      </tbody>
        </table>
      </div>
    </div>

<!-- RETORNO -->
<div class="widget widget-table action-table">
  <div class="widget-header"> <i class="icon-thumbs-up"></i>
    <h3>Retorno</h3>
  </div>
  <div class="widget-content">
    <table class="table table-striped table-bordered">
     <center>
      <div class="returnEmail"></div>
      <div class="testEmail"></div>
      <div class="resultEmail" style="width:70%; height:100px; overflow:auto; padding:1%; border:1px solid; margin-top:1%;" >Emails Validos<br/></div>
     </center>
     <br>
    </table>
  </div>
</div>

in the textarea I put a great list of email:name being like this.

email:nome
email2:nome2
email3:nome3

so on and so forth

and send the post pro javascript

<script type="text/javascript">
  $(document).ready(function(){
  $('.resultEmail').hide();
  $('#submitValidacao').click(function(){
    var logins = $('#logins').val();
    var loginsSplited = logins.split("\n").reverse();
        var loginsSplited = logins.split("\n");

      var loginsSplitedCount = loginsSplited.length;
      $('.returnEmail').html("<hr><b>Testando "+loginsSplitedCount+" Login...<br>Aguarde...</b><br>");
      var counter = 0;

$.each(loginsSplited, function (i, val) {
        $('.testEmail').html("Testando agora = "val);
        $.post('pages/engines/email.php', {
            login: val
        }, function (retorna) {
            $('.resultEmail').show();
        $('.resultEmail').append(retorna);
            counter++;
            if (counter === loginsSplitedCount) {
                $('.returnEmail').html("<hr><b>Testando " + loginsSplitedCount + " Login...<br>Pronto!</b><br>");
                $('.testEmail').html("<font color='gren'>Teste finalizado com sucesso!</font>");
            };
        });
    });

  });
});

</script>

now in the $('.testEmail').html("Testando agora = "val); shows only Testing now = email:name that is the first value of the list that was added in the textarea, and when action ends it calls the $('.testEmail').html("<font color='gren'>Teste finalizado com sucesso!</font>");

replacing the Testing now = .

the problem is that I can not pass the values right in Testing now = , getting email:name and when it ends it already came Email2:Nome2 , because php already returns 1 by 1 ..

  • could show me the link ?

  • I think your problem here is the same as the other question I marked as duplicate. I mean, you have to have that if (counter === loginsSplitedCount){ inside function(retorna){ because ajax is asynchronous. http://answall.com/questions/60852/return-em-each-getjson-jquery

  • That I have done and I do not function and that question there does not answer my . even so thank you

  • I don’t think you understand the asynchronous nature yet. I reopened it for myself or someone to explain it to you. But the problem is asynchrony as in the other question.

  • I’ll try here Sergio, I’ll read here.

  • What I really want is just to send the post and get the information that php returns :( if I upload a large list it tells which one is testing I think I’m missing somewhere .

  • Try this: http://jsfiddle.net/z1qtj621/

  • From my point of view the reason is that by default javascript is asynchronous, what I suggest is to change to synchronous, so it will run the ajax and only after he has finished executing the if just below.

  • Following link with reply on synchronous. http://stackoverflow.com/questions/5821380/how-to-make-a-jquery-post-request-synchronous

Show 4 more comments

1 answer

3

What’s happening is that cycle $.each is running and finishing alone. With each iteration you have counter++, it adds up and comes to an end. After it will be run what the server returns.

After?? Yes, after that.

AJAX is asynchronous, which is why it’s inside your $.each it will call the callback function with the result that the server returns when the $.each is surely over.

How can I control the application flow?

You have to start with the ajax callback, because that’s where the code will be waiting and will be called when the server responds.

Suggestion:

    $.each(loginsSplited, function (i, val) {
        $('.testEmail').html("Testando agora = " + val);
        $.post('pages/engines/email.php', {
            login: val
        }, function (retorna) {
            $('.resultEmail').show();
            $('.resultEmail').append(retorna);
            counter++;
            if (counter === loginsSplitedCount) {
                $('.returnEmail').html("<hr><b>Testando " + loginsSplitedCount + " Login...<br>Pronto!</b><br>");
                $('.testEmail').html("<font color='gren'>Teste finalizado com sucesso!</font>");
            };
        });
    });
  • @Wender to see if I got it right. How it works but you wanted me to show one by one by deleting the previous one. That’s it?

  • Just what I need

  • @Wender but AJAX is very fast. You won’t even see the values changing, it will seem that it only shows the last one. Are you aware of this? If that’s the way you want it to be + loginsSplitedCount + for + val + in the code I gave in the reply.

  • and there is no possibility to put a setInterval ?

  • @Wender yes, um setTimeout http://jsfiddle.net/jsdyjc9u/

  • Testing 97 Login... Please wait... Testing now = [email protected]:1961 does not change :/ and at the end Testing 97 Login... Ready! Test completed successfully! , Thank you very much for helping me , but I will leave for it , this function is very complicated ...

  • 1

    @Wender Suggestion: marks this question as accepted and poses another new question with the problem that remains. I confess that I still do not understand what you want to do. Otherwise it would help to find the solution to this other problem.

Show 2 more comments

Browser other questions tagged

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