2017-03-17 6 views
0

私は4つの入力を持ち、データをPHPファイルに送るためにAjaxを使用しています: このデータをjsonファイルに追加する方法は興味がありますか?jsonファイルを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:"wjson.php", 
    data:"name="+nombre+"&surname="+surname+"&mobile="+mobile+"&email="+email, 
    success:function(data) { 

    } 
}); 

JSONファイル:(people.json)

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

wjson.phpファイル:

<?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 
?> 

経由でファイルpeople.jsonは私のサーバーに

答えて

0

ありますあまりにも多くの余分な仕事を既にした

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

とwjson.phpファイルを置き換えるありがとう、私がしようとします。) –

+0

あなたの歓迎:) –

0

jsonファイルにデータを追加するにはこれを試してください。

<?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); 
} 


?> 
+0

ありがとう、私は試してみます:) –

1
// 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); 
+0

ありがとう、私は試してみます:) –

0

この

<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> 

を試してみて、

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); 
} 
+0

ありがとう、私は試してみる:) –

関連する問題