How to start a function with onclick?

1

I have a script that basically reads a .log in real time and shows it on the screen, but the question is that it starts with the DOM, I would like it to start just from the click of the button, could you help me? / p>

<?php
if (isset($_GET['ajax'])) {
    $sessionID = 'log'.$_GET['s'];
    session_id($sessionID);
    session_start();
    $handle = fopen('/\cordas\instance-8480\log\server.log', 'r');
    if (isset($_SESSION['offset'])) {
        $data = stream_get_contents($handle, -1, $_SESSION['offset']);
        echo nl2br($data);
    } else {
        fseek($handle, 0, SEEK_END);
} 
  $_SESSION['offset'] = ftell($handle);
  exit();
}

  $randomSession = rand();
    $(document).ready(function( ) {
        $.repeat(1000, function() {
            $.get('automacao_tela.php?ajax&s=<?=$randomSession;?>', 
                function(data) {
                $('#tail').append(data);
            });
        });
    });

<form action="?automacao=ok" method="POST">
    <button type="submit" class="log-btn" >INICIAR PROCESSO</button>
</form>

<div id="tail" class="widget-stats-list-log">
    Starting up...
</div>

----- Adding code ----

<body>
    <div class="automacao-container">
        <div class="automacao-box">
            <div class="row">
                <div class=" col-sm-3">
                    <div class="stats-widget">
                        <div class="widget-header">
                            <h2>Automação</h2>
                        </div>
                        <div class="widget-stats-list" id="lerLog">
                            <form action="?automacao=ok" method="POST">
                                <button type="submit" class="log-btn">INICIAR PROCESSO</button>
                            </form>
                        </div>
                    </div>
                    <div class="stats-widget" style="height: 495px;">
                        <div class="widget-header">
                            <h2>Parâmentros</h2>
                        </div>
                        <div class="widget-stats-list">
                        </div>
                    </div>
                </div>

                <div class=" col-sm-9">
                    <div class="stats-widget">
                        <div class="widget-header">
                            <h2>Log de Automação</h2>
                        </div>
                        <div id="tail" class="widget-stats-list-log">
                            Starting up...
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $('#lerLog').on('click', function( ) {
            $.repeat(1000, function() {
                $.get('automacao_tela.php?ajax&s=<?=$randomSession;?>', 
                    function(data) {
                    $('#tail').append(data);
                });
            });
        });                               
  </script>
</body>
    
asked by anonymous 28.07.2017 / 13:39

1 answer

1

Matheus, you need to create an element that will be clicked to call the function.

<div id='lerLog'>Ao clicar aqui a leitura do log acontecerá</div>

Great! Done this put the function this way:

 $('#lerLog').on('click', function( ) {
        $.repeat(1000, function() {
            $.get('automacao_tela.php?ajax&s=<?=$randomSession;?>', 
                function(data) {
                $('#tail').append(data);
            });
        });
    });

Applied change:

$(document).ready()

This call means that every time the document is ready to use (When the page finishes loading completely) The function you created will happen. read more about .ready ()

Then we create the button, we define an id for it and we assign the call .on() that works with the element when different situations happen. In this case we are using 'click' (onclick) . Learn more about .on ()

Reading your code again, I came across the button with the text START PROCESS. If it is the one responsible for reading log add: id='lerLog' to function with the function.

    
28.07.2017 / 13:53