2016-10-13 10 views
0

私はJSの初心者です。以前はPHPに触れたことはありません。時間と数時間のためにSOを精練した後、私はこの部分を一緒に束ねました。私がしようとしているのは、JSONファイルに保存される2つのフィールドを持つ単純なフォームです。後続の各エントリをファイルの末尾に追加して、各JSONオブジェクトがその人の名前とそのコメントになるようにしたいと思います。Ajax> PHP> jsonにフォームデータを追加

以下のコードでは、何かをPHPスクリプトに渡し、jsonファイルにエントリを追加できました。しかし、どのようなJSONファイルに表示(2つのエントリの後に)これ​​です:ここで

[{"data":"$data"},{"data":"$data"}] 

は私のHTMLは

 <form method="POST"> 

      <!-- NAME --> 
      <div id="name-group" class="form-group"> 
       <label for="name">Name</label> 
       <input type="text" class="form-control" name="name" placeholder=""> 
       <!-- errors will go here --> 
      </div> 

      <!-- EMAIL --> 
      <div id="comment-group" class="form-group"> 
       <label for="comment">Comment</label> 
       <input type="text" class="form-control" name="comment" placeholder=""> 
       <!-- errors will go here --> 
      </div> 

      <button type="submit" class="btn btn-success">Submit <span class="fa fa-arrow-right"></span></button> 

     </form> 

私のjqueryの

$(document).ready(function() { 
    // process the form 
    $('form').submit(function(event) { 
     // get the form data 
     // there are many ways to get this data using jQuery (you can use the class or id also) 
     var formData = { 
      'name'    : $('input[name=name]').val(), 
      'comment'    : $('input[name=comment]').val(), 
     }; 
     // process the form 
     $.ajax({ 
      type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
      url   : 'data/save.php', // the url where we want to POST 
      data  : formData, // our data object 
     }) 
      // using the done promise callback 
      .done(function(data) { 
       // log data to the console so we can see 
       console.log(formData); 
       // here we will handle errors and validation messages 
      }); 
     // stop the form from submitting the normal way and refreshing the page 
     event.preventDefault(); 
    }); 
}); 

そして、私のPHP

<?php 
if(!empty($_POST)){ 
$data = json_encode($_POST); 
if(json_last_error() != JSON_ERROR_NONE){ 
exit; 
} 
$file = file_get_contents('comments.json'); 
$data = json_decode($file); 
unset($file); 
$data[] = array('data'=>'$data'); 
file_put_contents('comments.json',json_encode($data)); 
unset($data); 
} 
です

?>

ありがとうございます

+0

を。行を扱う方法が分かりません – Aeromax

+0

変数は二重引用符で囲まれ、一重引用符ではなく展開されています。ちょうど 'bash'と' perl'のように。 – Barmar

+1

あなたは '$ data'を引用符で囲む必要は全くありません。 'array( 'data' => $ data)'を使用してください。 – Barmar

答えて

0

$dataは、変数が展開されないようにしています。配列にユーザ入力パラメータを入れていない場合は、配列$dataを使用して配列を配置しようとしています。

$data[] = array('name' => $_POST['name'], 'comment' => $_POST['comment']); 

あなたは文字列に変数を補間する場合は、二重引用符ではなく、単一引用符を使用する必要があります。 What is the difference between single-quoted and double-quoted strings in PHP?

json_encode($_POST)というコードの部分は表示されません。ポスティングできるデータはすべてエンコードできるため、ポストデータの有効な検証は行われません。

また、FormDataをJavascriptで使用する必要はありません。これは通常、ファイルをアップロードする場合にのみ必要です。他の入力のために、あなたは.serialize()メソッドを使用することができます:私は(かなり確信して)それは$データ[] =配列(「データ」=>「$データ」)とは何かを持って知っている

var formData = $(this).serialize(); 
+0

私は先に行って試してみましたが、今は私のjsonファイルに[{"data":null} – Aeromax

+0

なぜ '$ data'を配列に入れていますか?これはファイルから読み込んだ配列を保持する変数です。ユーザー入力を配列に入れてはいけませんか? – Barmar

+0

入力は '$ _POST ['name']'と '$ _POST ['comment']'にあります。 – Barmar

関連する問題