dpitqpax07626 2018-11-12 18:17
浏览 329
已采纳

使用JavaScript在文本文件中写入数据

I'm working on a project to make sure that users finish a video. I would like to have it just add something like "user has finished video" to an already existing text file. Here is what I have in my JavaScript file.

    var video = document.getElementById("video");

var timeStarted = -1;
var timePlayed = 0;
var duration = 0;
// If video metadata is laoded get duration
if (video.readyState > 0)
getDuration.call(video);
//If metadata not loaded, use event to get it
else {
video.addEventListener('loadedmetadata', getDuration);
}
// remember time user started the video
function videoStartedPlaying() {
timeStarted = new Date().getTime() / 1000;
 }

function videoStoppedPlaying(event) {
// Start time less then zero means stop event was fired vidout start event
if (timeStarted > 0) {
    var playedFor = new Date().getTime() / 1000 - timeStarted;
    timeStarted = -1;
    // add the new ammount of seconds played
    timePlayed += playedFor;
}
document.getElementById("played").innerHTML = Math.round(timePlayed) + "";
// Count as complete only if end of video was reached
if (timePlayed >= duration && event.type == "ended") {
    document.getElementById("status").className = "complete";
}
}

function getDuration() {
duration = video.duration;
document.getElementById("duration").appendChild(new Text(Math.round(duration) + ""));
console.log("Duration: ", duration);
}

video.addEventListener("play", videoStartedPlaying);
video.addEventListener("playing", videoStartedPlaying);

video.addEventListener("ended", videoStoppedPlaying);
video.addEventListener("pause", videoStoppedPlaying);



var data = "This user has finished the video";
var url = "data.php";
var http = new XMLHttpRequest();
http.open("POST", url, true);

//sends hearder info along with the request
http.setRequestHeader("content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
    alert(http.responseText);

}
}
http.send(data);

and Data.php has

    <?php
    $data = $_POST['data'];
    $file = fopen('names.txt', 'a');
    fwrite($file, $data);
    fclose($file);
    ?>

As of now, there are no errors in the console, but it does not write the data to the text file.

Please let me know what i'm doing wrong

  • 写回答

1条回答 默认 最新

  • drmg17928 2018-11-12 18:29
    关注

    Since you are using http.setRequestHeader("content-type", "application/x-www-form-urlencoded");, the request expects the data to be formatted like serialized HTML form data. Change the following line to provide the data in the proper format:

    var data = "data=This%20user%20has%20finished%20the%20video";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog