duanke8011 2015-10-18 16:05
浏览 291

如何将表单数据添加到JSON文件?

Right now I have this form:

<form name='steampaypalinfo' action='profile.php' method='post'>
PayPal Email: <br><input type='email' name='paypal'><br>
Steam Trade URL: <br><input type='text' name='tradeurl'><br><br>
<input type='submit' value='Update' name='submit'>
</form>

I am retrieving the data in PHP like this:

if (isset($_POST['submit'])) {
if (empty($_POST['paypal'])) {
    $paypalerror = "PayPal email is required!";
} else {
    $paypalemail = $_POST['paypal'];
}

if (empty($_POST['tradeurl'])) {
$tradeurlerror = "Steam Trade URL is required!";
} else {
    $tradeurl = $_POST['tradeurl'];
}

As you can see, I am storing the form data (email and link) into two variables called paypalemail and tradeurl.

Now I want to add this data into a JSON file that I have already made. The JSON file looks like this right now:

{
"response": {
    "players": [
        {
            "steamid": "76561198064105349",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "PUGLORD",
            "lastlogoff": 1445136051,
            "commentpermission": 2,
            "profileurl": "http://steamcommunity.com/id/ashland3000/",
            "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65.jpg",
            "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65_medium.jpg",
            "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65_full.jpg",
            "personastate": 0,
            "realname": "Jakob",
            "primaryclanid": "103582791439857810",
            "timecreated": 1337817157,
            "personastateflags": 0,
            "loccountrycode": "US",
            "locstatecode": "NY"
        }
    ]

}
}

I want the data paypalemail and tradeurl data to go into the players array. I read about using file_put_contents or fwrite but none of that seemed to work.

Question: How can I add the PayPal and Steam URL Data into the JSON file that is already made? How can I add that data on to the already made data in the JSON file and format it correctly?

Thank you, any help would be great!

EDIT I tried this:

$file = file_get_contents("cache/players/{$steam64}.json");
$json = json_decode($file, true);
$player_array['paypalemail'] = $paypalemail;

$json = json_encode($player_array);
$file = fopen("cache/players/{$steam64}.json", 'w');
fwrite($file, $json);
fclose($file);

It works but it overwrites the data in the JSON file. How can I add it but not overwrite it?

  • 写回答

1条回答 默认 最新

  • donlih2986 2015-10-18 16:57
    关注

    Should be something like this:

    $file = file_get_contents('players.json');
    $json = json_decode($file, true); //second parameter, return as associative array
    $player_array = &$json['response']['players'][0];
    //lets setup new keys
    $player_array['paypalemail'] = "johanroure@cool.com";
    //whatever you want to add
    $new_json = json_encode($json,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); //pretty and without slashes
    $file = fopen('players.json', 'w'); //w set pointer to beginning and truncate to 0 :D
    fwrite($file, $new_json);
    fclose($file);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能