0

Working from previous question: Create log text file using Javascript I'd like to log click events from a news scroller. The event detection works and triggers the alert.

    $("a.marq").click(function(){
    var url = $(this).attr('href');
    $.ajax({
      type:'POST',
      data:"ClickedButton="+url, 
      url: 'logger.php',
      success: function(data){
        //alert('req: '+url);
      }
      }); // END Ajax 
   });

Taking the php code from the referenced question, I created the logger:

   <?php
     // File: logger.php
    $myFile = "clicklog.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $_POST['ClickedButton'];
    fwrite($fh, $stringData);
    fclose($fh);
  ?>

The click events trigger the alert and display the correct data, but nothing is logged by the server. I checked server logs and nothing seemed to trapped. The server is running Apache mod_security, nothing in the logs that I could spot. It's simple "crawl-before-you-run" code, but I was tired of crawling and posted the question. I put the logger.php in the url root, touched the clicklog.txt file.

8
  • try for data: {'ClickedButton': url} and secondly check on your browsers console for errors, if its clean check for post data. And try to add error: function(jqXHR jqXHR, String textStatus, String errorThrown) {//handle error here} Commented Mar 31, 2015 at 5:09
  • @Franky Franky, added data: {'ClickedButton': url}, error: function(jqXHR, textStatus, errorThrown) { alert('An error occurred... '); No errors. Didn't match your suggestion, but close, sorry, This is new to me. Still no errors or logging. Any suggestions? }, Commented Mar 31, 2015 at 6:00
  • does it gives an error..? Commented Mar 31, 2015 at 6:01
  • No errors in console or alert. Commented Mar 31, 2015 at 6:06
  • try it in a different browser chome... Commented Mar 31, 2015 at 6:14

1 Answer 1

0

Apache write permissions, need g+apache, 774. Also needs 'a' not 'w' for appending to log file, $fh = fopen($myFile, 'a') or die("can't open file"); Thx to commenters for error handling and other changes that may have helped.
Working code:

     $("a.marq").click(function(){
        var url = $(this).attr('href');
        $.ajax({
        type:'POST',
        data: {'ClickedButton': url}, 
       url: 'code/logger.php',
       error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... ');
            },
       success: function(data){
         //alert('req: '+url);
     }
     }); // END Ajax 
   });



    <?php
   // File: logger.php
   // ----------------------------------------------------------------------
     $myFile = "/xxxxx/code/click.log";
     $fh = fopen($myFile, 'a') or die("can't open file");
     $stringData = $_POST['ClickedButton'];
     fwrite($fh, $stringData);
     fclose($fh);
   ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.