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 2
Oldest  Newest  Rating
0 Marie Goodwyn · November 17, 2014
I am so grateful for your time and help. I am still getting null. I used the get the string for the selector but now I am not getting anything there either. It seems that params from the jquery does not get into $param in the php.
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 18, 2014
Hello wheately and thank you so much for your help. I know how this feels since I am one of the top contributors on the Edge Animate forum. Is there a way to give you credit for this answer? 

So, It does post to the file which is great progress!!! 
However the data is not quite in the format that I need for Edge Animate. Currently the format for the json file is and should remain as below. I expected stringify to format the data properly. (fake data below)

            "question":"What is the color of charcoal",
   "feedbackInfo":"Charcoal is usually black and has many usage applications."
            "question":"What is the color of sand",
   "feedbackInfo":"Sand is usually yellow but can also be white or grey."

and what I got from your file is:


Which contains extra quotation marks and \. I wonder if there is a way to get ride of the unwanted stuff. Ideally what is between {} should be added and followed by a comma as shown in the example above. If the format is different, I will not be able to use the json file with Edge Animate. 

This form is a avoid having teachers write their own json file since it is a little complicated for non-coder!
0 Marie Goodwyn · November 18, 2014
So I have extra \ and "". I wonder if there is a way to get the right formatting for json. Also when it appends it seems to append after the last ] which brakes the usable data. If I append each question separately it will not work. I wonder if there would be a way to add all the questions in the html page and create a new json file at the end with all the data at once. I guess it would be easier to respect the formatting this way.
0 Marie Goodwyn · November 18, 2014
It seems that I am not getting this.  When I open the data file I get this data:

"{\"question\":\"What is the color of charcoal?\",\"answer1\":\"red\",\"answer2\":\"blue\",\"answer3\":\"black\",\"answer4\":\"white\",\"correctAnswer\":\"3\",\"image\":\"charcoal\",\"feedbackInfo\":\"The natural color of charcoal is black.\"}"
0 Marie Goodwyn · November 18, 2014
Oops, sorry I did not see the previous post. Let me look at it.
0 Marie Goodwyn · November 18, 2014
You're the BEST! It is totally working as I wanted and I learned a lot in the process. :)
Now what I have several things I want to do:
1- clear the input field on submit so they can add the new answer.
2- a way to edit the questions later. Maybe I can create a table that shows all the questions and a way to edit them and re-post them to the file.

This should be an interesting project and of course it will be slow for me since I am very new at this.
0 Marie Goodwyn · November 18, 2014
Great. Thanks. So I got the loading of data to view it working fine. Could you point me to a way to make it editable in my table.
Here is what I think:
I should have input fields in the table and then a submit button to update the data.
Is this right?
0 Marie Goodwyn · November 18, 2014
One question at a time would be great.

BTW, I noticed that the feedbackInfo data is not loaded. I looked at all the code and I am not sure why. Could you check it out?
0 Marie Goodwyn · November 18, 2014
There is a problem with the feedbackInfo data that does not get changed in the json file. All the variables are correct so I am not sure why.

I think it would be nice if they could edit any question. Right now I have a table where all the questions are loaded.

Also, I probably need a way to prevent no data from being submitted.



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