使用ajax请求将数据从作用域插入到PHP变量中

时间:2017-05-31 11:13:25

标签: php angularjs

我正在寻找一种方法将范围数据从输入插入到php变量,这就是我的代码的样子:

HTML:

<div class="form-group" ng-app="Filter" ng-controller="InsertFilter">
<form>
    <label for="limit">Limit:</label>
    <input type="text" class="form-control" id="limit" name="limit" ng-model="limit">
    <label for="page">Page:</label>
    <input type="text" class="form-control" id="page" name="page" ng-model="page">
    <label for="from">From:</label>
    <input type="date" class="form-control" id="from" name="from" ng-model="from">
    <label for="to">To:</label>
    <input type="date" class="form-control" id="to" name="to" ng-model="to">
    <button type="submit" class="btn btn-default" style="margin-top: 15px;">Show Report</button>
</form>

角度中的Ajax请求:

var FilterApp = angular.module('Filter', []);
      FilterApp.controller('InsertFilter', function($scope, $http) {
      $http.post("/api/data-dashboard.php").then(function(response){
      $scope.limit = /* ??? */;
  });
});  

数据dashboard.php:

if (empty($_POST["from"])) {
$from = date('Y-m-d');

} else {
$from = $_POST["from"];
}

if (empty($_POST["to"])){
$week = time() + (7 * 24 * 60 * 60);
$to = date('Y-m-d', $week);
} else {
$to = $_POST["to"];
}

if (empty($_POST["page"])) {
$page = 1;
} else {
$page = $_POST["page"];
}

if (empty($_POST["limit"])) {
$limit = 10;
} else {
$page = $_POST["page"];
}  

感谢各种回复 *我应该将输入传输到json格式吗?

1 个答案:

答案 0 :(得分:0)

从Angular HTTP Request使用

收集所有详细信息
List<Carrier> values = map.values().stream()
                                   .flatMap(List::stream)
                                   .sorted(comparator)
                                   .collect(Collectors.toList());

并将参数传递给$ http.post

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$from = request->from;