Khthulhu
Khthulhu

Reputation: 23

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. Problem is that the php only writes the time and date to the end of the file and nothing else. I took the example from a previous post and modified it here for my purposes. Here's the 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:

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
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. I'm not sure how else to proceed. Any helop would be appreciated. Thx!

Upvotes: 0

Views: 1215

Answers (2)

THE ONLY ONE
THE ONLY ONE

Reputation: 2190

try this code in 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

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

Upvotes: 1

Exlord
Exlord

Reputation: 5391

you are getting a empty $_POST variable so your php code is never gets executed. 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

Upvotes: 0

Related Questions