将表单数据从angular传递到php文件

时间:2016-09-21 11:17:57

标签: javascript php angularjs

成为Angular的新手可能会很痛苦。我试图从角形式获取信息到php文件。

以下是HTML表单:

<form ng-submit() = "sendRequest()" ng-controller="testCtrl" class="customform" action="time.php"  data-ng-init="init()">


<div class="s-10 1-10"><input ng-model="firstName" pattern=".{2,100}" title="2 to 100 Characters" placeholder="First Name" type="text" required /></div>
<div class="s-10 1-10"><input ng-model="lastName" pattern=".{5,255}" title="5 to 255 Characters" placeholder="Last Name" type="text" required /></div>
<div class="s-12 l-10"><input ng-model="acNumber" placeholder="A/C Number" title="Employee Number, i.e c1234567" type="text" required /></div>
<div class="s-12 l-10"><input ng-model="email" placeholder="Email" title="Email" type="email" required /></div>


    <div class="s-12 l-10">
        <select ng-model="selectedRequestType" ng-options="requestType as requestType.type for requestType in requestTypes">
            <option value="" disabled selected>Request Type</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedRequestType.type == 'Work Request'" ng-model="selectedWorkRequestType" ng-options="workRequestType as workRequestType.type for workRequestType in workRequestTypes">
            <option value="" disabled selected>Work Request Type</option>
        </select>
    </div>
<div class="s-12 l-10">
    <select ng-show="selectedWorkRequestType.type == 'New file(s) from source'" ng-model="selectedFile" ng-options="file as file.type for file in files">
        <option value="" disabled selected>Files</option>
    </select>
</div>

<div class="s-12 l-10">
    <select ng-show="selectedWorkRequestType.type == 'New file(s) from source'" ng-model="selectedLibrary" ng-options="library as library.type for library in libraries">
        <option value="" disabled selected>Does Library Exist</option>
    </select>
</div>
    <div class="s-12 l-10">
        <select ng-show="selectedWorkRequestType.type == 'Code Automation' || selectedWorkRequestType.type == 'Amendments to existing code'" ng-model="selectedOutput" ng-options="outputType as outputType.type for outputType in outputTypes">
            <option value="" disabled selected>Output</option>
        </select>
    </div>

    <div class="s-12 l-10">
        <select ng-show="selectedLibrary.type == 'Yes' || selectedRequestType.type == 'Incident'" ng-model="selectedPlatformOutput" ng-options="platformOutput as platformOutput.type for platformOutput in platformOutputs">
            <option value="" disabled selected>Platform Output</option>
        </select>
    </div>

    <div class="s-12 l-10">
         <input ng-show="selectedOutput.type == 'SAS' || selectedPlatformOutput.type =='SAS'" ng-model="sasLibraryName" type="text" placeholder="SAS Library Name: SPDS Exploit" />
    </div>

    <div class="s-12 l-10">
        <input ng-show="selectedOutput.type == 'SAP IQ' || selectedPlatformOutput.type =='SAP IQ'" ng-model="sapIQtext" placeholder="SAP IQ" >
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedOutput.type == 'Hadoop' || selectedPlatformOutput.type =='Hadoop'" placeholder="Library Name: HDFS_Exploit" ng-model="" />
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedWorkRequestType.type == 'Amendments to existing code'" placeholder="Output Dataset Name" ng-model="outputDataset" type="text"/>
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedLibrary.type == 'No'" type="text" ng-model="productName" Placeholder="Product Name" />
    </div>
    <div class="s-12 l-10">
        <input ng-show="" placeholder="Upload Text File" type="file" ng-model="uploadTextFile" title="Please upload a txt file with the layout - to " multiple />
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedRequestType.type == 'Incident'" type="text" ng-model="tableName" placeholder="Dataset/Table Name" />
    </div>
    <div class="s-12 l-10">
        <textarea placeholder="Special Instructions" ng-model="specialInstruction" rows="5"></textarea>
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedRequestType.type == 'Incident'" ng-model="uploadScreenshot" placeholder="Upload Screenshot of error" type="file" multiple/>
    </div>

    <div class="s-12 l-10">
        <select ng-show="selectedRequestType.type == 'Work Request'" ng-model="selectedFrequency" ng-options ="frequency as frequency.type for frequency in frequencies">
            <option value="" disabled selected>Frequency</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedFrequency.type == 'Weekly'" ng-model="selectedWeekly"  ng-options ="weekly as weekly.type for weekly in weeklies">
            <option value="" disabled selected>Weekly Frequency</option>
        </select>
    </div>

<input type="hidden" ng-model="token" value="<?php echo Token::generate();?>">
<div class="s-4"><button type="submit" id="submit" class="btn-custom btn btn-large btn-block">Request</button></div>
</form>

以下代码是控制器:

var app = angular.module('app', []);

app.controller('testCtrl', function($scope) {



$scope.init = function (){

  $scope.workRequestType = 'test';
  $scope.requestTypes = [
                        {'type':'Work Request'},
                        {'type': 'Incident'}                    
                        ];

  $scope.workRequestTypes = [
                              {'type': 'Amendments to existing code'},
                              {'type': 'Code Automation'},
                              {'type': 'New file(s) from source'}
  ];

  $scope.outputTypes = [
                    {'type': 'SAS'},
                    {'type':'SAP IQ'},
                    {'type': 'Hadoop'}
  ];

  $scope.frequencies = [
                        {'type' : 'Daily'},
                        {'type': 'Monthly'},
                        {'type': 'Weekly'}
  ];

  $scope.weeklies = [
                    {'type': 'Monday'},
                    {'type':'Tuesday'},
                    {'type': 'Wednesday'},
                    {'type':'Thursday'},
                    {'type':'Friday'},
                    {'type':'Saturday'},
                    {'type':'Sunday'}
  ];

  $scope.files = [
      {'type': 'New File(s)'},
      {'type': 'Add to existing file received'}

  ];

  $scope.libraries = [
      {'type':'Yes'},
      {'type':'No'}
  ]

  $scope.platformOutputs = [
      {'type': 'SAS'},
      {'type':'SAP IQ'},
      {'type': 'Hadoop'}
  ]


  $scope.firstName= null;
  $scope.lastName= null;
  $scope.acNumber= null;
  $scope.email= null;
  $scope.selectedRequestType= null;
  $scope.selectedWorkRequestType= null;
  $scope.selectedOutput= null;
  $scope.sasLibraryName = null;
  $scope.sasIQtext = null;
  $scope.selectedFrequency = null;
  $scope.selectedWeekly = null;
  $scope.selectedFile = null;
  $scope.selectedLibrary = null;
  $scope.selectedPlatformOutput = null;
  $scope.productName = null;
};

$scope.sendRequest = function(){
    var data= {

        'firstName' :$scope.firstName,
        'lastName' :$scope.lastName,
        'acNumber' :$scope.acNumber,
        'email' :$scope.email,
        'selectedRequestType' :$scope.selectedRequestType,
        'selectedWorkRequestType' :$scope.selectedWorkRequestType,
        'selectedOutput' :$scope.selectedOutput,
        'selectedFrequency' : $scope.selectedFrequency,
        'selectedWeekly' : $scope.selectedWeekly,
        'selectedFile' : $scope.selectedFile,
        'selectedLibrary' : $scope.selectedLibrary,
        'selectedPlatformOutput' : $scope.selectedPlatformOutput,
        'productName' : $scope.productName

    };
};




 });

以下是on time.php

      $data = file_get_contents("php://input");
    $request = json_decode($data);
   var_dump($request);

var_dump输出为NULL。我对角度很陌生,不知道我在哪里错过了这一点。请协助

1 个答案:

答案 0 :(得分:2)

使用$http向/从服务器获取/发送数据。

在Angular模块中添加$ http依赖

var app = angular.module('app', []);  
app.controller('testCtrl', ['$scope', '$http', function($scope, $http) {
    $http({
        url: "process.php",
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        data: data
    }).success(function(data, status, headers, config) {

    }).error(function(data, status, headers, config) {

    });
}]);

服务器端

php://input允许您阅读原始POST数据。

它是$ HTTP_RAW_POST_DATA的内存密集型替代品,不需要任何特殊的php.ini指令。

PHP wrappers

$data = json_decode(file_get_contents("php://input"));
var_dump($data);

建议的做法

要从表单获取多个值,您可以创建一个对象作为范围变量,然后将该对象传递给服务器,而不是声明并设置/获取控制器中的所有值。

<input type="text" ng-model="user.firstname" />
<input type="text" ng-model="user.lastname" />
<input type="text" ng-model="user.email" />

您可以直接访问控制器中的数据,如下所示

$scope.user //That will have all the three defined values of ng-model

更多帮助here

编辑:纠正问题并创建工作示例

您的代码几乎没有问题,请尝试以下方式。

<强> HTML

  • 您没有在HTML
  • 中定义ng-app
  • ng-submit() = "sendRequest()"应为ng-submit="sendRequest()"
  • 无需在表单中应用操作,因为您将使用$http
  • 传递表单数据
  • 您的代码有Error: ngModel:nonassign Non-Assignable Expression,因为其中一个输入的ng-model为空ng-model=""而不是ng-model="hadoop"

查看工作代码段,控制台表单数据是否成功。

var app = angular.module('app', []);
app.controller('testCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.init = function (){
        $scope.workRequestType = 'test';
        $scope.requestTypes = [
                    {'type':'Work Request'},
                    {'type': 'Incident'}                    
        ];
        $scope.workRequestTypes = [
                    {'type': 'Amendments to existing code'},
                    {'type': 'Code Automation'},
                    {'type': 'New file(s) from source'}
        ];
        $scope.outputTypes = [
                    {'type': 'SAS'},
                    {'type':'SAP IQ'},
                    {'type': 'Hadoop'}
        ];
        $scope.frequencies = [
                    {'type' : 'Daily'},
                    {'type': 'Monthly'},
                    {'type': 'Weekly'}
        ];
        $scope.weeklies = [
                    {'type': 'Monday'},
                    {'type':'Tuesday'},
                    {'type': 'Wednesday'},
                    {'type':'Thursday'},
                    {'type':'Friday'},
                    {'type':'Saturday'},
                    {'type':'Sunday'}
        ];
        $scope.files = [
                    {'type': 'New File(s)'},
                    {'type': 'Add to existing file received'}
        ];
        $scope.libraries = [
                {'type':'Yes'},
                {'type':'No'}
        ];
        $scope.platformOutputs = [
                {'type': 'SAS'},
                {'type':'SAP IQ'},
                {'type': 'Hadoop'}
        ];
        $scope.firstName= null;
        $scope.lastName= null;
        $scope.acNumber= null;
        $scope.email= null;
        $scope.selectedRequestType= null;
        $scope.selectedWorkRequestType= null;
        $scope.selectedOutput= null;
        $scope.sasLibraryName = null;
        $scope.sasIQtext = null;
        $scope.selectedFrequency = null;
        $scope.selectedWeekly = null;
        $scope.selectedFile = null;
        $scope.selectedLibrary = null;
        $scope.selectedPlatformOutput = null;
        $scope.productName = null;
    };
    $scope.sendRequest = function(){
        var data= {
            'firstName' :$scope.firstName,
            'lastName' :$scope.lastName,
            'acNumber' :$scope.acNumber,
            'email' :$scope.email,
            'selectedRequestType' :$scope.selectedRequestType,
            'selectedWorkRequestType' :$scope.selectedWorkRequestType,
            'selectedOutput' :$scope.selectedOutput,
            'selectedFrequency' : $scope.selectedFrequency,
            'selectedWeekly' : $scope.selectedWeekly,
            'selectedFile' : $scope.selectedFile,
            'selectedLibrary' : $scope.selectedLibrary,
            'selectedPlatformOutput' : $scope.selectedPlatformOutput,
            'productName' : $scope.productName
        };
        console.log(data);return false;
    };
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
<form ng-submit="sendRequest()" ng-controller="testCtrl" class="customform" data-ng-init="init()">
    <div class="s-10 1-10">
        <input ng-model="firstName" pattern=".{2,100}" title="2 to 100 Characters" placeholder="First Name" type="text" required />
    </div>
    <div class="s-10 1-10">
        <input ng-model="lastName" pattern=".{5,255}" title="5 to 255 Characters" placeholder="Last Name" type="text" required />
    </div>
    <div class="s-12 l-10"><input ng-model="acNumber" placeholder="A/C Number" title="Employee Number, i.e c1234567" type="text" required /></div>
    <div class="s-12 l-10"><input ng-model="email" placeholder="Email" title="Email" type="email" required /></div>
    <div class="s-12 l-10">
        <select ng-model="selectedRequestType" ng-options="requestType as requestType.type for requestType in requestTypes">
            <option value="" disabled selected>Request Type</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedRequestType.type == 'Work Request'" ng-model="selectedWorkRequestType" ng-options="workRequestType as workRequestType.type for workRequestType in workRequestTypes">
            <option value="" disabled selected>Work Request Type</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedWorkRequestType.type == 'New file(s) from source'" ng-model="selectedFile" ng-options="file as file.type for file in files">
            <option value="" disabled selected>Files</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedWorkRequestType.type == 'New file(s) from source'" ng-model="selectedLibrary" ng-options="library as library.type for library in libraries">
            <option value="" disabled selected>Does Library Exist</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedWorkRequestType.type == 'Code Automation' || selectedWorkRequestType.type == 'Amendments to existing code'" ng-model="selectedOutput" ng-options="outputType as outputType.type for outputType in outputTypes">
            <option value="" disabled selected>Output</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedLibrary.type == 'Yes' || selectedRequestType.type == 'Incident'" ng-model="selectedPlatformOutput" ng-options="platformOutput as platformOutput.type for platformOutput in platformOutputs">
            <option value="" disabled selected>Platform Output</option>
        </select>
    </div>
    <div class="s-12 l-10">
         <input ng-show="selectedOutput.type == 'SAS' || selectedPlatformOutput.type =='SAS'" ng-model="sasLibraryName" type="text" placeholder="SAS Library Name: SPDS Exploit" />
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedOutput.type == 'SAP IQ' || selectedPlatformOutput.type =='SAP IQ'" ng-model="sapIQtext" placeholder="SAP IQ" >
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedOutput.type == 'Hadoop' || selectedPlatformOutput.type =='Hadoop'" placeholder="Library Name: HDFS_Exploit" ng-model="hadoop" />
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedWorkRequestType.type == 'Amendments to existing code'" placeholder="Output Dataset Name" ng-model="outputDataset" type="text"/>
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedLibrary.type == 'No'" type="text" ng-model="productName" Placeholder="Product Name" />
    </div>
    <div class="s-12 l-10">
        <input ng-show="" placeholder="Upload Text File" type="file" ng-model="uploadTextFile" title="Please upload a txt file with the layout - to " multiple />
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedRequestType.type == 'Incident'" type="text" ng-model="tableName" placeholder="Dataset/Table Name" />
    </div>
    <div class="s-12 l-10">
        <textarea placeholder="Special Instructions" ng-model="specialInstruction" rows="5"></textarea>
    </div>
    <div class="s-12 l-10">
        <input ng-show="selectedRequestType.type == 'Incident'" ng-model="uploadScreenshot" placeholder="Upload Screenshot of error" type="file" multiple/>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedRequestType.type == 'Work Request'" ng-model="selectedFrequency" ng-options ="frequency as frequency.type for frequency in frequencies">
            <option value="" disabled selected>Frequency</option>
        </select>
    </div>
    <div class="s-12 l-10">
        <select ng-show="selectedFrequency.type == 'Weekly'" ng-model="selectedWeekly"  ng-options ="weekly as weekly.type for weekly in weeklies">
            <option value="" disabled selected>Weekly Frequency</option>
        </select>
    </div>
    <input type="hidden" ng-model="token" value="12">
    <div class="s-4"><button type="submit" id="submit" class="btn-custom btn btn-large btn-block">Request</button></div>
</form>
</body>  

现在你的angular有表单数据,你只需要使用$http将这些数据发送到服务器(PHP),把这段代码放在你控制数据的sendRequest函数上。

$http({
    url: "time.php",
    method: "POST",
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: data
}).success(function(data, status, headers, config) {
    //Success code
}).error(function(data, status, headers, config) {
    //Failure code
});

最后,在你的计时器php文件中获取数据。

<?php
  $data = json_decode(file_get_contents("php://input"));
  print_r($data->firstName);
  exit;
?>

希望这肯定能帮到你:)。