0

I have 4 input, which sent by Ajax 4 data, to a php file: how can I load json file and then add new data whih php?

<input type="text" id="name">
<input type="text" id="surname">
<input type="text" id="mobile">
<input type="text" id="email">
<script>
var name = $("#name").val();
var surname = $("#surname").val();
var mobile = $("#mobile").val();
var email = $("#email").val();
$.ajax({type:"POST",
url:"fill.php",
data:"name="+nombre+"&surname="+surname+"&mobile="+mobile+"&email="+email,
success:function(data) {

} }); JSON file: (people.json)

{
"1":
{
    "Name" : "Jhon",
    "Surname" : "Kenneth",
    "mobile" : 329129293,
    "email" : "[email protected]"
},
"2":
{
    "Name" : "Thor",
    "Surname" : "zvalk",
    "mobile" : 349229293,
    "email" : "[email protected]"
}
}

here I have a mistake fill.php file :

<?php
$name = $_POST['name'];
$surname =$_POST['surname'];
$mobile = $_POST['mobile'];
$email =$_POST['email'];
$file = 'people.json';
$data = json_decode(file_get_contents($file));
$newdata = array('name'=>$name, 'surname' => $surname, 'mobile'=>$mobile,'email'=>$email);
$data[] = $newdata;
file_put_contents($file, json_encode($data));
?>

when I exeute it, it delete all the people.json date, and everytime that I add new data, it give me the next result: [{},{},{},{}]

6
  • the first time, when you exceute it, it works ok?? Commented Jan 27, 2013 at 0:46
  • you have had several posts about this json file. Why not change the data structure in the file so that it matches what you need in page and don't have to map the data in browser to another format. Then you will be working with same structure in both javascript and php Commented Jan 27, 2013 at 0:46
  • yeah, but then when I fill the form, it delete all the people.json data Commented Jan 27, 2013 at 0:46
  • 2
    try json_decode(file_get_contents($file), true); this should return an array no matter what. Commented Jan 27, 2013 at 0:49
  • 2
    @dualed yeah, you right it works ok now Commented Jan 27, 2013 at 0:55

2 Answers 2

1

you need to add a secnod parameter to json_decode so it becomes an array, try this

<?php
$name = $_POST['name'];
$surname =$_POST['surname'];
$mobile = $_POST['mobile'];
$email =$_POST['email'];
$file = 'people.json';
$data = json_decode(file_get_contents($file),1);
$newdata = array('name'=>$name, 'surname' => $surname, 'mobile'=>$mobile,'email'=>$email);
$data[] = $newdata;
file_put_contents($file, json_encode($data));
?>
Sign up to request clarification or add additional context in comments.

4 Comments

what's the difference between json_decode(file_get_contents($file), true) and son_decode(file_get_contents($file),1);, is the same?
@Kakitori 1 and true do the same thing, same as 0 and false do are the same :)
@Kakitori as a side note, your JSON array should start on 0 not on 1 :) just a suggestion
what you mean?? i have to change json_decode(file_get_contents($file),1) by json_decode(file_get_contents($file),0)??
0

json decode accepts a second parameter, set it and you get arrays not stdClass objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.