Home>
$(function () {
    $("# send"). click (function (event) {
     var form_data = {
        id: $('# id'). val (),
        product: $('# product'). val (),
        color: $('# color'). val (),
  };
      var json = JSON.stringify (form_data);
      $.ajax ({
        type: "POST",
        url: "form.php",
        data: {json},
        dataType: "text"
      }). done (function (data) {
                $("# result"). text (data);
      }). fail (function (XMLHttpRequest, textStatus, errorThrown) {
        alert (errorThrown);
      });
    });
    });
<? php
$id = $_POST ['json'];
$fp = fopen ("form.txt", "a");
   fwrite ($fp, $id);
   fclose ($fp);
$text = file_get_contents ("form.txt");
$text_array = array ();
$text_array = explode ("\ n", $text);
$json = json_encode ($text_array);
echo $json;
 ?>


Currently registered in the txt file like the above code, but if you register with this code
{"id": "1", "product": "pen", "gender": "red"} etc., but this is registered
I want to register in the last line of the text file as ID = 1, product = pen, color = red.
What should I do?

  • Answer # 1

    I don't know why name is product and gender is color
    Also, if you throw at post, we recommend formdata.
    (It is not normal to send it as json)

    
    id:
    name:
    gender:

    form.php

  • Answer # 2

    Unverified

  • Answer # 3

    var json = JSON.stringify (form_data); Should I process it after decoding the json string received on php into an array?

    For example, ugly sample:

    $value) {
        if ($var! == '') {$var. = ',';}
        $var. = $key. '='. $value;
    }
    echo $var;
      

    id = 1, product = pen, gender = red