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 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿