Skip to content

Instantly share code, notes, and snippets.

@gs-ysingh
Created June 23, 2015 17:33
Show Gist options
  • Save gs-ysingh/ffc782d29f323d155da1 to your computer and use it in GitHub Desktop.
Save gs-ysingh/ffc782d29f323d155da1 to your computer and use it in GitHub Desktop.
// define angular module/app
var formApp = angular.module('formApp', []);
// create angular controller and pass in $scope and $http
function formController($scope, $http) {
// create a blank object to hold our form information
// $scope will allow this to pass between controller and view
$scope.formData = {};
// process the form
$scope.processForm = function () {
$http({
method: 'POST',
url: 'index.php',
data: $.param($scope.formData), // pass in data as strings
headers: { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
})
.success(function (data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorName = data.errors.name;
$scope.errorSuperhero = data.errors.superheroAlias;
}
else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment