I'm trying to save to JSON some form data via AngularJS $http.post but somehow it isn't working. The idea is pretty simple: I have a form with three fields, an AngularJS data-binding and a pretty simple PHP code to save it to a file. Somehow it isn't working because the PHP file doesn't get any input apparently, so it always stores null
.
Here is the code:
HTML Form:
<form class="form-horizontal">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title:</label>
<div class="col-md-4">
<input type="text" class="form-control" id="title" ng-model="book.title" placeholder="Title">
</div>
</div>
<div class="form-group">
<label for="pages" class="col-sm-2 control-label">Pages:</label>
<div class="col-md-1">
<input type="number" class="form-control input-sm" id="pages" ng-model="book.pages" placeholder="0">
</div>
</div>
<div class="form-group">
<label for="readPages" class="col-sm-2 control-label">Read pages:</label>
<div class="col-md-1">
<input type="number" class="form-control input-sm" id="readPages" ng-model="book.readPages" placeholder="0">
</div>
</div>
<button class="btn btn-primary glyphicon glyphicon-floppy-save" ng-click="save()"></button>
</form>
The JS code:
angular.module('book', [
'ui.router'
])
.config(function config($stateProvider) {
$stateProvider.state('book', {
url: '/book',
views: {
"main": {
controller: 'Book',
templateUrl: 'ui/views/book.tpl.html'
},
"header": {
templateUrl: 'ui/views/header.tpl.html'
},
"footer": {
templateUrl: 'ui/views/footer.tpl.html'
}
},
data: {pageTitle: 'Book Status'}
});
})
.controller('Book',
function ($scope, $http) {
$scope.book = {};
$scope.error = "";
$scope.save = function() {
console.log($scope.book);
$http.post('save/book.php', $scope.book).then(function(response) {
// log success
console.log("Guardado con éxito.");
console.log(response);
});
};
});
And the PHP code:
$data = file_get_contents("php://input");
$inp = file_get_contents('../data/books.json');
$tempArray = json_decode($inp);
$tempData = json_decode($data);
array_push($tempArray, $tempData);
$jsonData = json_encode($tempArray);
$file = fopen('../data/books.json','w+');
fwrite($file, $jsonData);
fclose($file);
echo $jsonData;
So, apparently everything is correct, but I'm getting this response:
( ! ) Warning: array_push() expects parameter 1 to be array, null given in C:\\wamp\\www\\angulardemos\\save\\book.php on line 7
Which tells me that the input is incorrect, so I'm not actually sending any data. What am I missing?
easy $tempArray
is null add var_dump to debug it
$tempArray = $tempArray = json_decode($inp, true);
$tempData = json_decode($data);
array_push($tempArray, $tempData);
You can also try:
$tempArray[]=$tempData;
Remove book from ng-model and make it to <input type="text" class="form-control" id="title" ng-model="title" placeholder="Title">
likewise for all.. and in the save function var dataObj = { title: $scope.title, pages: $scope.pages
}var dataObj = { title: $scope.title, pages: $scope.pages
}var dataObj = { title: $scope.title, pages: $scope.pages
}var res = $http.post('save/book.php', JSON.stringify(dataObj)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.