0

I have HTML form, and I want to send textarea entered values to jquery array and using for loop execute the ajax. my concern is I want to execute it one after one, once I got success to call back then it should increment. please my code below

    <form id="SendData" method="post">
      <textarea name="userids" id="DataArray" cols="10" rows="7"></textarea>
      <button type="submit" name="submit">submit</button>
   </form>

and my jQuery

<script>
$('#SendData').on('submit',function(e){
    e.preventDefault();

    var dataarray = $('#DataArray').val().split(",");

    for(var i=0; i<dataarray.length){

        $.ajax({
            type:'post',
            url:'reparepoola.php',
            data:{nuserid:dataarray[i]},
            success: function(result){
                if(result){
                    alert(result);
                    i++;
                    }else{
                        alert("end loop");
                        brake;
                        }
                }

            });

            }
    });
</script>

my loop is not working properly my only concern is that it should execute one after one. I tried setinterval but no use. I want to delay the loop for 1sec but its exciting in the middle of the loop. if I have more then 20 values in the array.

my reparepoola.php file is successfully sending back the results when I check with 2 values. more than that it's not working properly.

my reparepoola.php

include('dbhconfig.inc.php');


if($_POST){

         $newuserid = $_POST['nuserid'];

      $repare = $dbh->prepare("SELECT * FROM members WHERE userid='$newuserid'");
      $repare->execute();

        while($reparerow=$repare->fetch(PDO::FETCH_ASSOC)){

          $sponsor =$reparerow['sponsorid'];
          $todaydate=$reparerow['doj'];
          $userid = $reparerow['userid'];

          include('sponsorcomission.php');

          include('poola.php');

          echo $newuserid;  
         }

    }
Asesha George
  • 2,232
  • 2
  • 32
  • 68
  • Possible duplicate of [jQuery: Performing synchronous AJAX requests](https://stackoverflow.com/questions/6685249/jquery-performing-synchronous-ajax-requests) – Mark Baijens Nov 27 '18 at 15:20
  • Synchronous ajax is deprecated for very good reasons. – Pointy Nov 27 '18 at 15:21
  • 2
    the .ajax method is asynchronous so the loop will continue processing while the call to our reparepoola.php file is happening. There is a parameter to set to make the call synchronous if that is required although it would be better to pass all the data in as one ajax call rather than loop and call. – MikeS Nov 27 '18 at 15:21
  • in my PHP file, I am inserting the values into MySQL. if I send all the values its inerting too fast and my values are not inserting in a correct way. that's why I want to insert one after one. – Asesha George Nov 27 '18 at 15:37
  • what about in `$.ajax().then()`? http://api.jquery.com/jquery.ajax/ for more info – tonoslfx Nov 28 '18 at 10:57

0 Answers0