doudou8783 2016-12-11 17:27
浏览 40
已采纳

将单击坐标保存到服务器上的文件

I am currently outputting the the user click co-ordinates to the console and saving the file manually. I am trying to output these coordinates to a json file on the server which will be updated every time the user clicks.

<script type="text/javascript">
var clickX;
var clickY;
  onmousemove = function(e){
    clickX = e.clientX;
    clickY = e.clientY;
    console.log('X: '+clickX+', Y: '+clickY);

    var clicks = {"x": "clickX", "y": "clickY"}

    $.ajax({
      type : "POST",
      url : "save_json.php",
      data : {
          json : JSON.stringify(clicks)
      }
  });
  }
</script>

<?php
$myFile = "clicks.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["data"];
fwrite($fh, $stringData);
fclose($fh)
?>

However I am not managing to do so with my current code.

  • 写回答

1条回答 默认 最新

  • dtds8802 2016-12-11 19:09
    关注

    There are quite a few things that you're probably missing here. Firstly the ajax call makes a POST request but you're looking for the data in the $_GET array, so lets make the first change of the ajax request to make a GET request over here. That makes your ajax call look as follows

    $.ajax({
            type : "GET",
            url : "save_json.php",
            data : {
                json : JSON.stringify(clicks)
            }
        });
    

    Similarly, another issue is in var clicks = {"x": "clickX", "y": "clickY"} where instead of passing clickX and clickY parameters you're passing them as strings "clickX" and "clickY". This can be fixed by changing it to

    var clicks = {"x": clickX, "y": clickY};
    

    Lastly the event that you're using onmousemove records each and every single move made by the cursor and not really the clicks, based on the variable names and the question I believe you want the clicks, the event you're looking for is onmouseup

    Now the javascript code looks as follows with the corrections made

    <script type="text/javascript">
    var clickX;
    var clickY;
    onmouseup = function(e){
        clickX = e.clientX;
        clickY = e.clientY;
        console.log('X: '+clickX+', Y: '+clickY);
    
        var clicks = {"x": clickX, "y": clickY};
    
        $.ajax({
            type : "GET",
            url : "save_json.php",
            data : {
                json : JSON.stringify(clicks)
            }
        });
    }
    </script>
    

    Now coming to the PHP part, you need to modify the script as follows, the data that you're sending gets sent as array(1) { ["json"]=> string(17) "{"x":269,"y":125}" } on every click, the list item you're looking for is "json" and not "data", So making the correction to the script as follows

    <?php
    $myFile = "clicks.json";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $_GET["json"];
    fwrite($fh, $stringData);
    fclose($fh)
    ?>
    

    With the right permissions on the clicks.json file, you should be able to make an entry in the file which will look as follows

    {"x":798,"y":123}
    

    and will keep getting overwritten after every click because of the w mode used in fopen()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?