dptiq46022 2016-04-15 05:57
浏览 39
已采纳

PHP句柄json发送POST并用新的json重写现有的json

I'm trying to update json file on the server on button click. So when i click the button #update I'm collecting values from input fields and insert them to json object then I sent this json object to controller.php. But how to handle this json object delete the old json file and and create new json file with the json object in it.

Lets say the json file path is : js/currencies.json

JS:

function ajaxPost(obj){
        $.ajax({
                type: "POST",
                dataType: "json",
                url: "controller.php",
                data: {myData:obj},
                contentType: "application/json; charset=utf-8",
                success: function(data){
                    alert('Items added');
                },
                error: function(e){
                    console.log(e.message);
                }
        });
    }

    $(document).ready(function(){       
                $('#update').on('click', function(){
                    var objrates = 
                    {
                        "EURbuy" : $('#eurbuy').val(),
                        "EURsell" : $('#eursell').val(),
                        "USDbuy" : $('#usdbuy').val(),
                        "USDbuy" : $('#usdbuy').val()
                    }
                    ajaxPost(objrates);
                });                                                                                                                                                                                                                         
            });

PHP :

    <?php   
    $data[] = $_POST['myData'];
    $inp = file_get_contents('js/currencies.json');
    $tempArray = json_decode($inp);
    array_push($tempArray, $data);
    $jsonData = json_encode($tempArray);
    file_put_contents('js/currencies.json', $jsonData);
?>

The php code works withouth errors but the .json file is not replaced with the new values

  • 写回答

1条回答 默认 最新

  • dougua9165 2016-04-15 06:02
    关注

    Use json_encode to return the altered object or save it to a file with file_put_contents

    <?php 
        $obj = $_POST['myData'];
        $obj['key'] = 'newVal';//edith the values based on key
        echo $json = json_encode(array('newobj'=>$obj));//echo the json object if you want
         file_put_contents('js/currencies.json',$json); //replace the json with new json in the file
    ?>
    

    Note: you can even pass the file name to the ajax and retrieve to to append the it alter the data:

    data: {myData:obj,filename:'js/currencies.json'},
    

    php get the url and alter the file:

    $filename= $_POST['filename'];
    file_put_contents($filename,$json);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊