3

Being new to Angular can be painful. I'm trying to get information from an angular form to a php file.

Below is the HTML form:

<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>

Following code is the controller:

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

    };
};




 });

The following is on time.php

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

The var_dump output is NULL. I'm pretty new to angular and not sure where I'm missing the point. Please Assist

1 Answer 1

2
+50

Use $http to get/send data to/from server.

Add $http dependency in Angular module

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) {

    });
}]);

Server side

php://input allows you to read raw POST data.

It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.

PHP wrappers

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

Suggested practice

To get the multiple values from form, you can create an object as scope variable and just pass that object to the server rather then declaring and setting / getting all the values in controller.

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

You can directly access data in controller as below

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

More help here

Edit: Rectified the problems and created working example

Your code has few of problems, Try following way.

HTML

  • You didn't have ng-app defined in HTML
  • ng-submit() = "sendRequest()" should be ng-submit="sendRequest()"
  • No need to apply action in the form, because you will be passing form data using $http
  • Your code was having Error: ngModel:nonassign Non-Assignable Expression because one of the inputs was having ng-model blank ng-model="" instead of ng-model="hadoop"

See the working snippet, has console form data successful.

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>  

Now your angular has form data, you just need to send those data to server (PHP) by using $http, put this code on your sendRequest function where I've console the data.

$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
});

Finally, get data in your timer php file.

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

Hope this will definitely helps you :)

Sign up to request clarification or add additional context in comments.

9 Comments

still struggling. Adding the http section makes the form not to work. I added it under $scope.sendRequest
@kya: I've updated my answer, how to add http depedency in your controller under Edit-1 title, let me know if you still face any issue.
@kya : Everything works for you now or you still face any problem with dependency injection ?
it's just not coming right. could the issue be caused by my <form > tag ?
May be, make ng-submit="" not ng-submit()=""
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.