javascript
1$(function(){ 2 $("#send").click(function(event){ 3 var form_data = { 4 id:$('#id').val(), 5 product:$('#product').val(), 6 color:$('#color').val(), 7 }; 8 var json = JSON.stringify(form_data); 9 $.ajax({ 10 type:"POST", 11 url:"form.php", 12 data:{json}, 13 dataType: "text" 14 }).done(function(data){ 15 $("#result").text(data); 16 }).fail(function(XMLHttpRequest, textStatus, errorThrown){ 17 alert(errorThrown); 18 }); 19 }); 20 }); 21
php
1<?php 2$id = $_POST['json']; 3$fp = fopen("form.txt", "a"); 4 fwrite($fp, $id); 5 fclose($fp); 6 7$text=file_get_contents("form.txt"); 8$text_array = array(); 9$text_array = explode("\n",$text); 10$json = json_encode($text_array); 11echo $json; 12 ?>
現在上記のコードのようにtxtファイルに登録しているのですがこのコードで登録を行うと
{"id":"1","product":"pen","gender":"red"}といったように登録されてしまうのですがこれを
ID=1,product=pen,color=red というふうにテキストファイルの最終行に登録したいです。
どうやればよろしいですか?
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。