Arkorn Poonpon
Arkorn Poonpon

Reputation: 11

how to make json file with php

I have 4 input and using Ajax to send data, to a php file: Now i am curious how to append this data to the json file?

<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:"wjson.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]"
    }
}

wjson.php file :

<?php
$nane = $_POST['name'];
$surname =$_POST['surname'];
$mobile = $_POST['mobile'];
$email =$_POST['email'];
$str_datos = file_get_contents("people.json")
//add new data to people.json
?>

by the way people.json file is in my server

Upvotes: 0

Views: 64

Answers (4)

Ankit Singh
Ankit Singh

Reputation: 1477

try this

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <form>
    <input type="text" id="name">
    <input type="text" id="surname">
    <input type="text" id="mobile">
    <input type="text" id="email">
    <input type="button" id="save" value="Save" />
  </form>
<script>
 $(document).ready(function(){
    $('#save').click(function(){
        var name = $("#name").val();
        var surname = $("#surname").val();
        var mobile = $("#mobile").val();
        var email = $("#email").val();
        $.ajax({
            type: "post",
            url: "wjson.php",
            data : {name : name, surname : surname, mobile: mobile, email : email },
            success:function(data) {
                console.log(data);
            },
            error:function(data){
                console.log(data);
            }
        });
    });
});
</script>

and replace wjson.php file with

if(file_exists('people.json'))
{
    $data = $_POST;
    $people = file_get_contents("people.json");
    $people = json_decode($people,true);
    $json = json_encode(array_merge($people,array($data)));
    file_put_contents('people.json',$json);
}
else
{
    $json = json_encode($_POST);
    file_put_contents('people.json',$json);
}

Upvotes: 0

muhammad faizan altaf
muhammad faizan altaf

Reputation: 42

// in your wjson.php file
$name = $_POST['name'];
$surname =$_POST['surname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];

$data = array(
    'Name'=>$name,'surname'=>$surname,
    'mobile'=>$mobile,'email'=>$email
);
$prevData = file_get_contents("people.json");
$prevData = json_decode($prevData,true);
$jsonData = json_encode(array_merge($prevData,array($data)));
file_put_contents('people.json',$jsonData);

Upvotes: 1

Varun Malhotra
Varun Malhotra

Reputation: 1202

to append the data in json file try this.

<?php

if(file_exists("people.json")){

    $prevdata = file_get_contents("people.json");
    $newdata = $prevdata.','.json_encode($_POST);

    file_put_contents("people.json", $newdata);

}else{
    $data = json_encode($_POST);
    file_put_contents("people.json", $data);
}


?>

Upvotes: 0

Dimi
Dimi

Reputation: 1267

You did too much extra work already

<?php
$str_datos = file_get_contents("people.json",true);
$str_datos[]=$_POST;
file_put_contents('people.json',json_encode($str_datos);
?>

Upvotes: 0

Related Questions