problem writing to json file

+1 Marie Goodwyn · November 17, 2014
I am new at this so pardon my ignorance. I am trying to make a form to send data to a json file that will be later used to create a quiz with edge Animate. the quiz works fine with a json file I wrote. Now I want the teachers to be able to add questions to the json file from the form so they do not have to touch the json file.
I get the data posted to the html but not to the file.
Testing here:

Here is the code
if (!isset($_POST['params'])) {
        $params = $_POST['params'];
        $jsonObject = json_encode($params);
        file_put_content('myData.json', $jsonObject, FILE_APPEND);
echo '$jsonObject';


<script type="text/javascript">
     $(document).ready(function() {
         var postData = {
                question: $("#question").val(),
                answer1: $("#answer1").val(),
answer2: $("#answer2").val(),
answer3: $("#answer3").val(),
answer4: $("#answer4").val(),
correctAnswer: $("#correctAnswer").val(),
                image: $("#image").val(),
                feedbackInfo: $("#feedbackInfo").val()

var params = JSON.stringify(postData);
            type: "POST",
data: params, 
url: "submit.php",                  
            success: function (data) {
$("#myData").html("this data was posted: " + params);
error: function(e){


<form id="newQuestion" name="newQuestion" method="post">
<label for="question">Question:</label>
    <input id="question" name="question" type="text" /><br>
<label for="answer1">Answer 1 or distractor: </label>
    <input id="answer1" name="answer1" type="text" /><br>
<label for="answer">Answer 2 or distractor: </label>
    <input id="answer2" name="answer2" type="text" /><br>
<label for="answer">Answer 3 or distractor: </label>
    <input id="answer3" name="answer3" type="text"  /><br>
<label for="question">Answer 4 or distractor: </label><br>  
    <input id="answer4" name="answer4" type="text"  /><br>
<label for="correctAnswer">Correct answer number: </label>
    <input id="correctAnswer" name="correctAnswer" type="text" /><br> 
<label for="image">Image name (Exampe: for house.png simply enter: house): </label>
    <input id="image" name="image" type="text"  /> <br>
<label for="feedbackInfo">Info about the question: </label>
    <input id="feedbackInfo" name="feedbackInfo" type="text" /> <br>        
<input type="button" value="submit" name="submit" id="submit"/>  

Post a Reply


- page 1
Oldest  Newest  Rating
0 Marie Goodwyn · November 22, 2014
OK. This corrected the problem and the edit file did not need to change. So this is so cool. 
I have been swamped with work and I still have a lot of work this weekend so I have not totally gone through all the files to try to understand the php and javascript code but this is totally on my agenda as well as finishing the tutorials here and try to write some sample files for practice  but your help has been amazing and I can so grateful you took the time to basically do it all.  
It is important because I will probably add some changes in the future as I am expecting to add different types of questions in the quizzes. So the json structure will probably change since I will add questionType and each will have a different structure. 
But this is not for now so I am very happy with what I have now.
Again THANKS!!!
0 Marie Goodwyn · November 21, 2014
OK. I used your download php file and another one I found and either one shows the problem that it does not find the file. However the file is there and used for all the other scripts.

Do you think that json is not permitted for download maybe?

Other script here:

Testing site here:
0 Marie Goodwyn · November 21, 2014
Under no file I get this info:
So I think that the download.php might have something wrong but I do not know enough about php to know besides the fact that the path was written wrong.

After I corrected the path, I do get data into the file at Data/myData.json. However there is an id written to it. I am not sure whether this is normal. And still file in not found on the server.
The download script has another error since it mentions a pdf file not a json file. It still does not work however with this change.
Also, I used a different download script and it still cannot find the file though it is there and the path is right.

[{"id":"546f4af710b92","question":"What is the color of charcoal","answer1":"blue","answer2":"red","answer3":"black","answer4":"white","correctAnswer":"3","image":"charcoal","feedbackInfo":"The color of charcoal is black."},{"id":"546f4b37dea83","question":"What is the name of the American Emperor","answer1":"Washington","answer2":"Obama","answer3":"Regan","answer4":"Bush","correctAnswer":"2","image":"obama","feedbackInfo":"Obama thinks he is the emperor and many believe it."}]
-1 Marie Goodwyn · November 21, 2014
OK. I think the path is wrong using \ instead of / but even though I changed them in all the files I still cannot download.

The file cannot be found in the server. Is there an extra / in front?
-1 Marie Goodwyn · November 21, 2014
I tried FF and Chrome try to save the file, open the dialog box and fait. Safari and IE give a server error.

Here is what I have noticed looking at the server (godaddy) folders. The file is written at the root folder (testphp3) as Data\MyData.json.  and as you can see it is 583 bytes. However there is a file in the Data folder and it is 0 bytes. 

So I am thinking that something is wrong when the file is created and data is sent to it. 

0 Marie Goodwyn · November 21, 2014
I like the way everything is done - clean and simple. simple is always better and easier to understand!
0 Marie Goodwyn · November 21, 2014
I am not sure why it fails. I am on Windows 8.1. The save dialog box opens as you can see below but when I click save I get the failed- No file message.
0 Marie Goodwyn · November 21, 2014
This is pretty cool! I really want to learn php very well so I can expand my expertise! I see the great potential for what I do with Animate.
Some remarks:

  • I really like the idea of verifying the file though I do no see if and how it does it.

  • deleting a question works perfect. :) You'll have to tell me how it refreshes.

  • deleting all question works perfect! :)

  • The download fails though I entered several questions.

  • Otherwise all looks really good and I need to try and understand it all.
0 Marie Goodwyn · November 20, 2014
I am wondering about a few things:

  • is there a way I could give the users the way to save the json file so they have a copy to include in the quiz system? I know they could just type in the address and display it but I think it would be a security issue.

  • is there a way to delete all data in the file to start a new set of questions? 

  • is there a way to delete one question only?

  • is there a way to check if a question data is empty to delete it so there is no empty question in the quiz?

0 Marie Goodwyn · November 19, 2014
You have really gone the extra mile to help a total stranger and I will not forget it. 

I still have to finish the php tutorial here and I will probably go through it several times until I can make things work on my own. I guess it will take some time. I am mostly self-taught and learned JavaScript here with Bucky. I have recommended it several time on the Animate forum.  I will also carefully review your input above.

As you, it is my privilege to help others on the Edge Animate forum and it is rewarding in itself to see how happy the people we help are. I have received countless emails of "happy customers" and it is always nice to get them. 

When someone thanks me on the forum, I often end my post with: "Now, go help someone else!"  and some do!

So I hope some day I will be able to answer a php question! Who knows...



Server-side, HTML embedded scripting language used to create dynamic Web pages.