I'm using angular to retrieve json data via get request. I would like to update the json file similarly by post, and have PHP respond by sending back the updated json. The problem is that the page is not updating (ajax) when the json file is updated.
HTML
<html ng-app="myApp">
<body ng-controller="mainController">
<div>
<h3>Rules</h3>
<ul>
<li ng-repeat="rule in rules">{{rule.rulename}}</li>
</ul>
</div>
<div>
<label>New Rules:</label>
<input type="text" ng-model="newRule" />
<input type="button" ng-click="addRule()" value="Add" />
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('mainController', ['$scope','$http', function($scope, $scope.getItems = function() {
$http.get("localhost:8888/json/newRules.json")
.then(
function(response){
//success callback
$scope.rules = response.data;
},
function(response){
// failure callback
console.log(response);
}
);
};
$scope.getItems();
$scope.newRule = '';
$scope.addRule = function() {
$http.post("localhost:8888/json/newRules.json", JSON.stringify([{rulename: $scope.newRule}]))
.then(
function(response){
//success callback
$scope.getItems();
$scope.rules = response;
$scope.newRule = '';
},
function(response){
// failure callback
console.log(response);
}
);
};
}]);
PHP
<?php
$data = file_get_contents("newRules.json");
$data2 = file_get_contents("php://input");
$postData = json_decode($data);
$postData2 = json_decode($data2);
$newArray = array_merge($postData, $postData2);
$newPost = json_encode($newArray);
file_put_contents("newRules.json", $newPost);
?>