0

私はjsonファイルを持っていて、入力フィールドを含むリストとしてデータを表示します。すべてを編集できます。保存をクリックすると、新しいデータでjsonファイルを上書きしたいです

次のマークアップがあります

   <ul id="sortable">
      <li>
         <input class="jahr" type="number" value="2010" placeholder="Jahr">
         <input class="titel" type="text" value="testtitel" placeholder="Titel">
         <input class="delete" type="button" value="löschen">
     </li>
      <li>
         <input class="jahr" type="number" value="2012" placeholder="Jahr">
         <input class="titel" type="text" value="testtitel2" placeholder="Titel">
         <input class="delete" type="button" value="löschen">
     </li>
   </ul>

データをjsonファイルに保存したいので、次のjqueryコードがあります

var erfolge = [];

$('#sortable').children().each( function(){
    erfolge.push({titel : $(this).find('.titel').val(), jahr: $(this).find('.jahr').val()});                
});

$.ajax({
   type: "POST",
    url: "erfolge_speichern.php",
    dataType: 'json',
    data: { json: erfolge }
});

php ファイルは次のようになります。

<?
$json = $_POST['json'];

$file = fopen('../json/erfolge.json','w+');
fwrite($file, json_decode($json));
fclose($file);
?>

投稿データをfirebugで見るとこんな感じ

json[0][jahr]   2010
json[0][titel]  testtitel
json[2][jahr]   2012
json[2][titel]  testtiel2

json ファイルは空です

前もって感謝します!

4

0 に答える 0