Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to post data From angular js to zend controller?

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?

like image 292
krishnak Avatar asked Mar 27 '26 21:03

krishnak


1 Answers

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.

like image 200
BKM Avatar answered Mar 29 '26 09:03

BKM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!