Function to Log

Asked

Viewed 26 times

1

People took a code and it works perfectly notes the date and time as I modified so q open the page! However at the time of putting it in a function to be fired by a Button it n works! It must be something simple follows code:

    <?php

echo date('H:i:s');

function markLog(){
$fp = fopen('meus_links.txt', 'a');
fwrite($fp, date('H:i:s-'));
fclose($fp);
}

?>

<body>
    <button type="submit" onClick="markLog()" >Click</button>
</body>

1 answer

2

Cesar, there are differences there.

You have created a function in PHP and are calling it via javascript. This will not work. Ideally you would work with ajax for this. So you get the javascript function to access the page with php done.

PHP does not know Javascript PHP cannot directly call a Javascript function because PHP is interpreted on the server side.

PHP does not see a code there, it simply copies the text character by character and sends it to the user’s browser.

The browser knows Javascript When the browser reads the received data already in HTML, it starts to interpret this HTML and assemble the elements on the screen.

When finding a tag the nevegador stops what it is doing and executes what it has in this script.

Note that at this point, the browser is running the script on the user’s computer, while PHP (which has possibly finished running) was running on the server.

Well, I’ll help you with ajax.

1) php page: call it log.php

<?php

echo date('H:i:s');


$fp = fopen('meus_links.txt', 'a');
fwrite($fp, date('H:i:s-'));
fclose($fp);


?>

2) In your index copy this code:

<form id="simples-formulario-ajax" method="post">
    <button type="submit" name="enviar" value="enviar" id="enviar">
        Clique aqui para criar um log :)
    </button> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$('#simples-formulario-ajax').submit(function(e){
    e.preventDefault();

    if($('#enviar').val() == 'Enviando...'){
        return(false);
    }

    $.ajax({
        url: 'log.php',
        type: 'post',
        dataType: 'html',
        data: {
            'acao': 'log'           
        }
    }).done(function(data){
        alert("Log criado com sucesso");
    });

});
</script>

All right, working like a beauty :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.