If I understand the question correctly, then you display to the user a page with some content, in which there is a block with the #groupConsole selector, and your script with an AJAX request. Thus, but the moment of loading the page, all the content that is in the log file (as far as I understood from the php script) is already loaded.
Further, you want to add the last line of logs to your page dynamically, that is, after a certain time interval or on some event.
As you already answered Alexey Shimansky in the comments, when requesting via AJAX, you need to get only the last line from the log file and add it to the right place. And in order to dynamically add new lines that appear in the file, you need to periodically repeat your ajax request. For example, using the SetInterval or SetTimeout function (not strong in ajax requests, therefore I can’t give a normal working code).
Here, the specifics of the client-server technology - the client asked, the server answered. While the client is not asking, the server is not responding. And one more thing, the server, after the execution of the request, completely forgets everything that he transferred to the client, and, if not specifically informed, he does not know what the client has. So if you want the server to report only those lines that the client does not have, then you need to either send information through the request that the client already has, or use sessions, etc.
Continued.
I, like you, are just learning web development, so my code is far from a complete solution, but as an example, my code may work.
Code index.php
<?php $ch = file('logs.txt'); for ($i = 0, $l = count($ch) - 1; $i < $l; ++$i) { $result .= '<div>' . $ch[$i] . '</div>'; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test page</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <div id="groupConsole"> <?php echo $result; ?> </div> <script type="text/javascript"> function SendGet() { //отправляю GET запрос и получаю ответ $.ajax({ type: 'get', //тип запроса: get,post либо head url: 'ajax.php', //url адрес файла обработчика data: { 'q':'1' }, //параметры запроса response: 'text', //тип возвращаемого ответа text либо xml success:function (data) { //возвращаемый результат от сервера $('#groupConsole').append(data); } }); } setTimeout(SendGet, 3000); </script> </body> </html>
Ajax.php code
<?php header("Content-type: text/txt; charset=UTF-8"); $ch = file('logs.txt'); if (isset($_GET['q'])) { if ($_GET['q'] == 1) { echo '<div>' . trim($ch[count($ch) - 1]) . '</div>'; } } ?>
When the page is loaded into the #groupConsole block, the first three lines are added from the logs.txt file (there are 4 lines in total), then, after 3 seconds, an AJAX request occurs and the returned data is added as a new div block inside the #groupConsole block.
echo $ch[count($ch) - 1]and no cycles are needed .... - Alex Shimansky