3

The solution to this question most likely involves databasing, but unfortunately Squarespace does not play well with many databases yet such as mySQL.

My question is if there is any way or code that I can implement without setting up a database to capture some information (username, ip address, location, timestamp) when a user on the site clicks a 'submit' button and pipe it to a log file? I'm sure there has to be, and I apologize for not having any code related to my question ready, I'm still researching solutions. I can provide the jQuery code for the button:

<body>
  <div id="popup" align="right">
    <object type="text/html" id="terms" data="/popup-form" width="60%" height="90%" style="overflow: auto">
    </object><br>
    <form>
      <input id="cancel" type="button" value="Cancel" onClick="history.go(-1);return true;"/>
      <input id="submit" type="submit" value="I Agree" />
    </form>
  </div>

  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
  <script>
    $("form").on("submit", function(e){
      e.preventDefault();
      $("#popup, #overlay").hide();
      $.cookie("popup", "displayed", { expires: 7 });
    });
    var hasSeenpopup = $.cookie('popup');
    if(!hasSeenpopup){
      $("<div>",{ id : "overlay" }).insertBefore("#popup");
      $("#popup").show();
    }
  </script>
</body>
  • don't use squarespace if you actually want to code - squarespace is for people who don't. –  May 05 '15 at 21:13
  • The decision's been made, so we need to implement something within the squarespace environment; there's no other option. I'm reading more about Parse Javascript SDK here: https://parse.com/docs/js/guide, does anyone know how well it works within Squarespace? – Khepfer Haru May 05 '15 at 22:00

1 Answers1

0

To send data with AJAX using jQuery use

$("form").on("submit", function(e){
    //your previous code
    $.ajax({
         type: "POST",
         url: "myform.php",
         data: {'form': $("form").serialize()},
         success: function(message) {
            //do whatever
         }
      });
   return false;
});

Afterwards process the $_POST in myform.php and write the data to some.log

$string = '';

$date = date('Y-m-d H:i:s');

$ip = '';
if (!isset($_SERVER['SERVER_ADDR'];))
   $ip = $_SERVER['SERVER_ADDR'];

$string = $date.' : '.$ip.PHP_EOL;

file_put_contents('some.log', $string,  FILE_APPEND);

Regarding location - there might be issues take some insight into this post.

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56