dongquming3255 2014-11-21 16:26
浏览 47
已采纳

将JS / HTML转换为基于Web的 - 保存点击数据的问题

I made this entire experiment with html/javascript. Basically, people have to click on a chart (I used jqplot) and the console.log saves the value on the axes (var xax and yax below) they choose.

'$('#chart').bind('jqplotClick', function(event, seriesIndex, pointIndex, data) {
var xax = pointIndex.xaxis;
    var yax = pointIndex.yaxis;
    console.log("Coordinates according to chart location for plot1 : " + xax + " - " + yax);'

This was supposed to go on mTurk, who takes care of all the data logging. Unfortunately, non-US users are no longer allowed, so I'll have to host it on my own server (server space is no prob).

I know that I can use PHP to make a text-file on the server to save things in. What I basically need is some sort of simple way where everything that was logged with console.log in js, would now be saved by php in a text-file. I also know that PHP is server-based and Javascript is client-based, so it won't be that 'simple' I guess.

I only started coding beginning of October so I'm still a newbie. I know html/css, javascript and now some php. I read some answers on the JS-PHP relationship involving AJAX but I don't know how to apply it to my problem..

  • 写回答

2条回答 默认 最新

  • douya5331 2014-11-21 16:52
    关注

    Since it looks like you're using jQuery, have a look at their ajax documentation.

    But it basically boils down to sending the data you want to the server from your client-side js:

    // validate the data before sending.  
    
    $.ajax({
      type: "POST",
      url: "saveToFile.php",
      data: { xax: xax, yax : yax }
    
    }).error(function(xhr, errStr, err) {
        console.log("NO! something bad happened while sending to the server",errStr);
    
    }).success(function(out) {
       var response = JSON.parse(out);
    
       if (response.status === 200) {
         console.log(response.message);
       }
       else if (response.status === 500) {
         console.log("data not saved, something happened while writing the file");
       }
    });
    

    and handling it in your php file (this one named saveToFile.php). My php is a little rusty, sorry for any syntax errors:

    <?php
    
        // get your data from the client
        $xax = $_POST['xax'];
        $yax = $_POST['yax'];
        $out;
    
        // validate $xax and $yax
    
        // save your variables to a file
        $result = file_put_contents("log.txt", "Coordinates according to chart location for plot1 : " . $xax . " - " . $yax, FILE_APPEND);
    
        // tell the user what happened
        if ($result !== FALSE) {
          $out = [ "message" => "success!", "status" => 200];
        }
        else {
          $out = [ "message" => "error saving file", "status" => 500];
        }
    
        // send stuff back to the client
        echo json_encode($out);
        exit();
    

    saving to file from the php docs.

    This is a really basic example and doesn't check to see if the values you are sending to the server make sense, which is something you should do. Whenever sending data to the server, you should always validate on both ends; before sending it with JS AND when receiving it with PHP.

    Hope that helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看