Pause loop on page

0

I have a problem, which I researched and thought how to do, but I could not solve. I have select on the page that needs to be populated after a condition is satisfied . And after that, it needs to query the data relative to the option chosen in the select. Suppose that this select is to fill in the users name, and after it is filled in, a query must be made in the database with the data relative to that user. And that's where my problem was, even though I can fill out the select and bring in the data for the chosen user, every time the page is started, the script runs again and keeps checking every time . It's kind of a query loop that I can not seem to stop. How can I make only one query be done and then stop it so that the query is not repeated again ?

  

It's more a problem of logic than of syntax, which is why I have reduced   the code itself.

    <script>
$(document).ready(function () {
    if (condicao) {
        $.ajax({
            // execução do ajax, aqui preencho o select
        });
        // aqui está o problema, ao preencher o select, é para submeter o form. Porém isso tem causado loop na consulta
    }
    })
    </script>

    <form method="post">
    <select class='form-control' id='opcoes'>
    </select>
    </form>
    
asked by anonymous 28.07.2016 / 15:29

1 answer

1

Well, come on. From what I understand. After filling in the select, you'd like to submit the form, but it's still in Ajax's loop, right?!

What you can do to solve this is to create a "lock" variable and populate it in ajax success. I'll try to give an example:

    var stop = false;
<script>
$(document).ready(function () {
if (condicao) {
    if(!stop){
        $.ajax({
          method: "POST",
          url: "some.php",
          data: { name: "John", location: "Boston" }
        }).done(function() {
            stop = true;
          });
    }
    }
})
</script>

<form method="post">
<select class='form-control' id='opcoes'>
</select>
</form>

Here are examples of using functions at the end of ajax: link

I hope you have helped. Hugs

    
28.07.2016 / 16:13