doujiao9574 2017-05-18 18:50
浏览 92
已采纳

使用php从ajax发送的数据写入带有数组的json文件

I am sending a string and an array to a php file from an AJAX request

My html looks like this

<form>
  <input class="description" type="text">
  <input type="submit">
</form>

My js file looks like this

$('form').submit(function(e){

    e.preventDefault();

    var description = $('.description').val();

    var fileNames = ['1.jpg', '2.jpg'];

    var data = {
        description,
        fileNames
    };

    $.ajax({
        url: 'details.php',
        type: 'POST',
        data,
        success: function(data){
            console.log(data)
        }
    });
});

My php file looks like this

<?php

$str = file_get_contents("test.json");

// // decode JSON
$json = json_decode($str, true);

$decodedJSON =  json_decode($str, true);
var_dump($decodedJSON);

$description = $_REQUEST;

$milliseconds = round(microtime(true) * 1000);

$description->time = $milliseconds;

file_put_contents('test.json', json_encode($description, JSON_PRETTY_PRINT));

?>

This sets the json file as

{"description":"test text entered","SQLiteManager_currentLangue":"2"}

I want the json file to look like where the number is the current time in ms.

{
    "1495134004244": {
        "images": [
            "2.JPG"
        ],
        "description": "test"
    }
}
  • 写回答

1条回答 默认 最新

  • douxiong4892 2017-05-18 19:49
    关注

    You need to redo pretty much everything. I would do it in this manner:

    HTML:

    <form>
        <input type="text" name="description">
        <input type="hidden" name="images[]" value="1.jpg">
        <input type="hidden" name="images[]" value="2.jpg">
        <input type="submit">
    </form>
    

    JS:

    $('form').submit(function(e){
        e.preventDefault();
        $.ajax({
            url: 'details.php',
            type: 'POST',
            data: $(this).serialize(),
            success: function(data){
                console.log(data)
            }
        });
    });
    

    PHP:

    // get data from request
    $newArray = $_REQUEST;
    
    // get json from file
    $json = file_get_contents('test.json');
    
    // turn json into array
    $masterArr = json_decode($json, true);
    
    // get current time in milliseconds
    $milliseconds = round(microtime(true) * 1000);
    
    // use milliseconds as array key and use the new array as its value
    $masterArr["$milliseconds"] = $newArray;
    
    // turn array back to json
    $json = json_encode($masterArr, JSON_PRETTY_PRINT);
    
    // save json to file
    file_put_contents('test.json', $json);
    
    // echo the json so that you can use it in the AJAX call
    echo $json;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试