There are two files:
1) index.html
<form id="myForm"> <textarea rows="10" cols="45" name="field1"></textarea> <input type="submit" value="Отправить"> </form> <div id="content"></div> <script> $(document).ready(function(){ $('#myForm').submit(function(){ var str = $(this).serialize(); $.ajax({ type: "POST", url: "test.php", data: str, success: function(html){ $("#content").html(html); } }); return false; }); }); </script>
2) test.php
<?php $field1 = $_POST['field1']; echo "Приветствую, <b>".$field1."</b>!<br>"; echo "Time: " . date("H:i:s") . "<br/>" . "\n"; sleep(5); echo "Done"; ?>
Input data:
Маша Даша Петя Саша
Data is listed, separated by \ r \ n.
How to consistently transfer data using ajax to a php file so that the output is:
Приветствую, Маша Time: 10:06:00 ok Приветствую, Даша Time: 10:06:05 ok Приветствую, Петя Time: 10:06:10 ok Приветствую, Саша Time: 10:06:15 ok
Data should appear on the page gradually, in this case after 5 seconds, possibly receiving a call for a subsequent action after receiving echo "ok"; I suppose that first, from the data you need to get an array in js, with a separator for which will be \ n \ r, gradually feed php, first opening the session.
Note: the option of getting an array in php, its subsequent decomposition and sending to echo is not suitable, since with a large amount of incoming data there will be a high load, which will increase the wait and may cause an error, and there can be no talk about html .