3

view page:

<div ng-controller="LoginCtrl">
<form name="login_form" ng-submit="submit()" >
    Email: <input type="email" ng-model="login.email" required/><br />
    Password: <input type="password" ng-model="login.pass" required/><br />

    <input type="submit" />
</form>

main.js

function LoginCtrl($scope, $http) {
$scope.login = {};
$scope.submit = function(){

    $http({
        method: 'POST',
        url: '/login',
        data: $scope.login,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}

    }).
        success(function(response) {
            window.location.href = "/login";
        }).
        error(function(response) {
            $scope.codeStatus = response || "Request failed";
        });
}

LoginController:

if ($request->isPost()) {
            $data = json_decode(file_get_contents("php://input"));}

i need to fetch data from angular form and pass it to zend controller and perform login check and submit form.Could anybody help with step by step explanation?

krishnak
  • 141
  • 1
  • 8
  • To be a valid HTTP POST, don't you need to have a HTML name attached to the JSON submission? In the login controller, add `error_log( file_get_contents("php://input") );` – Owen Beresford Aug 19 '13 at 07:22
  • This question appears to be off-topic because it is about tutoring – Stewie Aug 19 '13 at 08:31

2 Answers2

4

Change your main.js like this;

function LoginCtrl($scope, $http) {
$scope.login = {email: "", password: ""};
$scope.login = $.param($scope.login);
$scope.submit = function(){

    $http({
        method: 'POST',
        url: '/login',
        data: $scope.login,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}

    }).
        success(function(response) {
            window.location.href = "/login";
        }).
        error(function(response) {
            $scope.codeStatus = response || "Request failed";
        });
}

In your php file access your email and password like;

$email = $_REQUEST['email'];
$password = $_REQUEST['password'];

and send these credentials to your zend controller. Hope it helps.

BKM
  • 6,949
  • 7
  • 30
  • 45
0

I found another post that talks about angular not sending data in a manner that php expects. Namely it is doing a json encoding. When I ran into this issue, the REQUEST was empty. Check out the following post:

AngularJs http post does not send data

Community
  • 1
  • 1
Scott
  • 7,983
  • 2
  • 26
  • 41