dtu15253 2016-12-23 12:13
浏览 58
已采纳

JSON新手 - 在浏览器重新加载之间永久保存值为JSON

I have an AJAX poll based on this tutorial: http://www.w3schools.com/php/php_ajax_poll.asp

However, instead of writing the poll entries from the HTML radio button to an array in a text file (as outlined in the tutorial). I would like to write to a JSON file. At the moment the values from the radio button entry are sent to the JSON, but the file does not retain the value between browser refreshes. I think that this is probably an issue with my syntax or encoding - the server should have the correct permissions to write to the file. Any tips are greatly appreciated.

<?php $vote = $_REQUEST['vote'];
//get content of json
$filename = "poll.json";
$poll = file_get_contents($filename);
$json = json_decode($poll);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0) {
$yes = $yes + 1;
}
if ($vote == 1) {
$no = $no + 1;
}
//insert votes to json file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
$poll = json_encode($json);
fclose($fp);?>

At the moment the result is either: {1||} or {||1} in the JSON file, I can't figure out how to save the values the way that they were saved to the .txt file version (as outlined here: http://www.w3schools.com/php/php_ajax_poll.asp)


Update

So based on the excellent advice I received I am much closer. I found that "w" was the correct option for the php as "a+" appended data in the JSON rather than updating the existing values.

I now have a JSON file which is updating the values that are added through the html radio buttons.

So the result looks like: [3,4] AKA 3 votes from option 1 and 4 votes for option 2

  • 写回答

3条回答 默认 最新

  • dtbi27903 2016-12-23 12:42
    关注

    Several issues:

    • You JSON format is not JSON. Values like 0||1 are not JSON, and so json_encode on it will fail. One of the benefits of true JSON is that you don't have to juggle with delimiters like ||.
    • The $content variable is nowhere initialised.
    • You encode something as JSON near the end, but don't do anything with it.
    • You call a variable $json when you intend to store decoded JSON in it. That is really confusing. Don't call such a variable $json.

    Here is some alternative code:

    $vote = $_REQUEST['vote'];
    //get content of json
    $filename = "poll.json";
    if (!file_exists($filename)) { // First time ever
        $poll = [0, 0];
    } else {
        $poll = json_decode(file_get_contents($filename));
        if (!$poll) $poll = [0, 0];
    }
    // increment counter for vote
    $poll[$vote] += 1;
    //insert votes to json file
    file_put_contents($filename, json_encode($poll));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用