2

I try run loop inside with some elements as for example post form , the idea it´s when the first post send , send the second and third , etc

<?php
$list="1,2,3";

$exp=explode(",",$list);

$b=0;
foreach($exp as $ids)
{ ?>
    <form action="" method="post" id="form_2">
    <input type="hidden" name="send_end" value="ok" />
    </form>
    <script>
    document.getElementById('form_2').submit();
    </script>
    <?php
    sleep (5);

    if ($b=="2")
    {
        exit("All Send");
    }
    $b++;
}

The problem it´s send all the same time and don´t works , the idea it´s send first 1 , wait , send 2 , etc , i use sleep but it´s the same , all time work sending all in one time

The question it´s how i can send in loop but sequentially

Regards and thank´s for the help

Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
Sergio R Sergi
  • 163
  • 1
  • 1
  • 7

2 Answers2

1

Your requirement can be achieved using javascript. Please check below snippet for more understanding.

var $list="1,2,3";

var $exp= $list.split(',');
var $b=0;

function createFromAndSubmit($exp,$b){
  alert("Form Create & submit - "+($b+1));
  var newForm = '<form action="" method="post" id="form_2"><input type="hidden" name="send_end" value="ok" /></form>';
  $("#newFrom").html(newForm);
  $('#form_2').submit();
  
  $b = $b+1;
  
  if($exp.length == $b){
    alert("finished");
  }else{    
    setTimeout(function() {      
      createFromAndSubmit($exp,$b);
    }, 5000);
  }
}

createFromAndSubmit($exp,$b);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="newFrom"></div>
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0

Your logic is wrong: You are submitting your forms using javascript but you are setting your time-out in php.

So the only thing you are doing, is delaying the generation of your html / javascript; it will arrive 15 seconds later at the browser and then all form submits will be triggered simultaneously.

If you want to use javascript to submit the forms, you should also use javascript timeout's to handle the delay between them.

And you should probably use ajax for the form submits because you don't want to reload your page every time when any of the forms is submitted...

The alternative in php is for example using cURL to make POST requests. Then you would not need javascript and you can handle the delay in php like you do now.

Edit: Also note that all of your forms have the same ID and ID's have to be unique. Now only the first (probably...) form will be submitted 3 times.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • I don´t want use javascript for nothing i only send or show elements inside when start 1 and end show the second , etc , forget the submit form , it´s possible show elements sequentially or no , my idea it´s inside loop show each time one element to the end of loop – Sergio R Sergi Sep 05 '16 at 10:13