so have log client-side script run times server side log file. measure time function takes, , report server via ajax. code correct? don't have access production server right now, xampp malfunctioning, , don't want show boss until i'm sure it'll work. first time using ajax, , second time using js.
in index.php:
function search(x,acc) { var starttime = new date().gettime(); //do work // var endtime = new date().gettime() - starttime; var outputmessage = "the process took: " + max/1000 + "seconds"; console.log(outputmessage); $.ajax({ type: "post", url: "ajaxcallback.php", data: {outputmessage}, success: function() { console.log("client side : ajax post submitted."); } } }
then ajaxcallback.php:
<?php $stringdata = $_post['outputmessage']; echo $stringdata; $myfile = "logfile.log"; $fh = fopen($myfile, 'a') or die("can't open file"); fwrite($fh, $stringdata); fclose($fh); ?>
your data
param in ajax
call needs use key/value
pair:
data: {outputmessage:outputmessage}
in outputmessage
, variable max
not defined.
also proper error handling should considered when using fopen
:
if($fh = fopen($myfile, 'a')){ fwrite($fh, $stringdata); fclose($fh); }else{ die("can't open file"); }
everything else looks pretty straight forward.
Comments
Post a Comment