I can't find on the web so I'm asking some help over here. I'd like to create a log file, when someone press on a button it will a text to a log.txt file. I'm just looking for the the base code based on Jquery ajax and PHP
Thanks
there is no base code for jquery logging to sever
I would recomend using log4javascript library with AjaxAppender, see example here, you can then use setBatchSize to send in batches or setTimed to send with time intervals, see documentation here, also you can setSendAllOnUnload to don't lose messages on browser close (in most cases).
in the server i would use log4php with LoggerAppenderRollingFile see here
I don't have to time to code it for you but here is the basic logic. You can look up how to do each item individually.
You need to add an event listener with jquery for the click event and bind it to the button.
Inside that, have an ajax request to some endpoint or script on your server that will update the log file.
When that script is executed (via the ajax call), you can have PHP open the file and append a line to it that has whatever data you want.
JavaScript:
$('button').on('click', function() {
$.ajax({
// do ajax request here
});
});
PHP:
$file = fopen("log.txt","rw");
$fileContents = fread($file, filesize("log.txt"));
fwrite($file, $fileContents."Someone clicked button\r\n");
fclose($file);
Something like that. Sorry for not giving a full explanation but I hope it helps.