【问题标题】:AngularJS: How to send html data to php?AngularJS:如何将 html 数据发送到 php?
【发布时间】:2016-10-20 22:29:17
【问题描述】:

我想将在我的 datepicker 输入中选择的日期发送到一个 php 变量,但我不知道如何在没有 angularJS 中的“提交”表单按钮的情况下执行此操作(以避免重新加载页面)。

我试过了,但没有成功:

我的php代码:

<?php
require('config-bd.php');

$filtros = json_decode(file_get_contents("php://input"));
$fec1 = $filtros->fec1;
$fec2 = $filtros->fec2;
?>

我的html代码:

<div class="form-group">
   <label for="desde" style="margin-left: 5px;">Desde:</label>
       <div class="input-group">
        <input name="fec1" type="text" class="form-control input-sm" uib-datepicker-popup ng-model="dt1"  is-open="popup1.opened" datepicker-options="dateOptions" close-text="Close" />
            <span class="input-group-btn">
                <button type="button" class="btn btn-sm" ng-click="open1()"><i class="fa fa-calendar"></i></button>
            </span>
        </div>
</div>
<div class="form-group">
   <label for="hasta" style="margin-left: 5px;">Hasta:</label>
        <div class="input-group">
          <input name="fec2" type="text" class="form-control input-sm" uib-datepicker-popup ng-model="dt2" is-open="popup2.opened" datepicker-options="dateOptions" close-text="Close" />
            <span class="input-group-btn">
                <button type="button" class="btn btn-sm" ng-click="open2()"><i class="fa fa-calendar"></i></button>
            </span>
        </div>
</div>

和我的控制器代码:

    $scope.today = function() {
        $scope.dt1 = new Date();
        $scope.dt2 = new Date();
    };
    $scope.today();

    $scope.clear = function() {
        $scope.dt1 = null;
        $scope.dt2 = null;
    };

    $scope.dateOptions = {
        formatYear: 'yy',
        startingDay: 1
    };

    $scope.open1 = function() {
        $scope.popup1.opened = true;
    };

    $scope.open2 = function() {
        $scope.popup2.opened = true;
    };

    $scope.setDate = function(year, month, day) {
        $scope.dt1 = new Date(year, month, day);
        $scope.dt2 = new Date(year, month, day);
    };

    $scope.popup1 = {
        opened: false
    };

    $scope.popup2 = {
        opened: false
    };

提前感谢:)

【问题讨论】:

标签: php html angularjs variables input


【解决方案1】:

您需要使用$http。通常,每当您必须发布一些数据时,您都必须通过 http post 调用来完成。您已经拥有 $scope.dt1 和 $scope.dt2 中的日期值。您只需捆绑并发送即可。

以下代码会将数据发布到占位符http post url

var data = [$scope.dt1, $scope.dt2];
$scope.sendDates = function(){

    $http.post('https://jsonplaceholder.typicode.com/posts',data)
     .then(function(response){
       console.log(response)
     })
}

类似地,要在您的 php 代码中使用它,您需要调用 php url。类似的,

var data = {date1: $scope.dt1,date2: $scope.dt2};
$scope.sendDates = function(){

    $http.post('someUrl/somePhp.php',data)
     .then(function(response){
       console.log(response)
     })
}

然后在 php 中,您需要从请求正文中读取值,

echo $_POST["date1"]

我还没有尝试过 php 代码,但它应该可以工作。关于占位符后呼叫的其余部分已经过测试和工作

【讨论】:

  • 谢谢!这非常有用! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多