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)
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
Several issues:
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 ||
.$content
variable is nowhere initialised.$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));
Your problem is at this line:
$fp = fopen($filename,"w");
According to the manual, mode "w":
places the file pointer at the beginning of the file and truncate the file to zero length
In other words: Each request overrides the whole file with it's contents.
To fix this, use mode "a+".
$fp = fopen($filename,"w");
Probably you change code and this is undefined:
$content[0]
In
$array = explode("||", $content[0]);
Source Is:
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//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 txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>