简体   繁体   English

尝试使用Ajax和PHP将JSON数据写入txt文件

[英]Trying to write JSON data to a txt file using Ajax and PHP

My project is to take input from the user and write it to the end of a text file in JSON format using ajax and php. 我的项目是从用户那里获取输入,并使用ajax和php将其以JSON格式写入文本文件的末尾。 Problem is that the php only writes the time and date to the end of the file and nothing else. 问题是php仅将时间和日期写入文件的末尾,而没有其他内容。 I took the example from a previous post and modified it here for my purposes. 我从以前的帖子中摘取了示例,并出于我的目的在此处进行了修改。 Here's the html movie.html: 这是html movie.html:

<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <script src="movie.js" type="text/javascript"></script>
    </head>

    <body>
        <h1>
            <center>
                <input id="box" type="textbox"name="box" value="Enter Movie:"/>
                <input id="add" type="button" value="Submit" onClick="addStuff();" /></center>
        </h1>
        <div id="status" ></div>

        <h2>MOVIE NAME:</h2>

        <ul id="list" name="list">
            </ul>
        <div id="status"></div>
    </body>
</html>

Here's the movie.js file which sends the data via Ajax: 这是movie.js文件,它通过Ajax发送数据:

function addStuff(){
var movie_name_entered = document.getElementById("box").value;
var movieList = document.getElementById("list");

var hr= new XMLHttpRequest();
var url= "movie.php";
hr.open("POST",url,true);
hr.setRequestHeader("Context-type","application/x-www-form-urlencoded");
var param = "film=" + movie_name_entered;
hr.setRequestHeader("Content-length", param.length);
hr.setRequestHeader("Connection", "close");
hr.onreadystatechange= function(){
    if(hr.readyState==4 && hr.status==200){
        var return_data=hr.responseText;
        console.log(hr.responseText);
        document.getElementById("status").innerHTML=return_data;
    }
}
hr.send(param);
document.getElementById("status").innerHTML = "processing...";
}

Here's the php (btw, I console.logged the data being sent to the php and it is correct): 这是php(顺便说一句,我在console.log中记录了发送到php的数据,它是正确的):

<?php
if($_POST){
$data = $_POST["film"];
$file ='movie.txt';
$fp = fopen($file, "a");
$encoded = json_encode($data);
fwrite($fp, $encoded);
fclose($fp);    
return $encoded;}
?>

As mentioned above, the code only writes the time and date to the text file and nothing more no matter what I do. 如上所述,该代码仅将时间和日期写入文本文件,而无论我做什么,仅此而已。 I tested the data being sent and it's valid $_POST data. 我测试了正在发送的数据,它是有效的$ _POST数据。 I'm not sure how else to proceed. 我不确定该如何进行。 Any helop would be appreciated. 任何坡道将不胜感激。 Thx! 谢谢!

try this code in movie.js 在movie.js中尝试此代码

function addStuff() {
    var movie_name_entered = document.getElementById("box").value;
    var movieList = document.getElementById("list");

    var hr = new XMLHttpRequest();
    var url = "movie.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var param = "film=" + movie_name_entered;
    hr.setRequestHeader("Content-length", param.length);
    hr.setRequestHeader("Connection", "close");
    hr.onreadystatechange = function() {
        if (hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            console.log(hr.responseText);
            document.getElementById("status").innerHTML = return_data;
        }
    }
    hr.send(param);
    document.getElementById("status").innerHTML = "processing...";
}

Please change your php code to below 请更改您的PHP代码到下面

if ($_POST) {
    $data    = $_POST["film"];
    $file    = 'movie.txt';
    $fp      = fopen($file, "a+");
    $encoded = json_encode($data);
    fwrite($fp, $encoded);
    fclose($fp);
    exit();
}

you are getting a empty $_POST variable so your php code is never gets executed. 您将获得一个空的$_POST变量,因此您的php代码将永远不会执行。 you have a mistake in your code : 您的代码有误:

hr.setRequestHeader("Context-type","application/x-www-form-urlencoded");

it should be Content-type , replace x with c :D 它应该是Content-type ,将x替换为c :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM