0

I am trying to send values from AngularJS to PHP like below

loginService.js

'use strict';

app.factory('loginService',function($http)
{
   return{
       login:function(user){
                var promise = $http.post('data/user.php',user); //send data to user.php
                promise.then(function(msg){
                    if(msg.data=='succes') console.log('succes login');
                    else console.log('error login');   
                })
       }
   }
});

My PHP file is like below

user.php

<?php
    $user=json_decode(file_get_contents('php://input'));

    if($user->mail=='foysal@gmail.com' && $user->pass=='1234')
    {   
        print 'succes';
    }
    else 
    {   
        print 'error';
    }
 ?>

My output is like below

error login

loginCtrl.js

'use strict';

app.controller('loginCtrl',function($scope,loginService)
{
    $scope.login=function()
    {
    loginService.login();
    }
})

Could anyone say where is my mistake??

Thanks

I am getting a message like this "<br />↵<b>Notice</b>: Trying to get property of non-object in <b>D:\php\htdocs\login_angularjs\app\data\user.php</b> on line <b>14</b><br />↵""

How can I debug here ?? Like, is php file getting values ?? is php file sending values ??

UPDATE

I think I failed to gather the values from the HTML Form. I am giving HTML Form code here.Could you help me to figure out the issue ??

login.tpl.html

Welcome : {{user.mail}}

<div class="bs-example">
    <form role="form" name="form1">
        <div class="form-group">
            <p>Welcome : {{user.mail}}</p>
            <label for="exampleInputEmail">Mail</label>
            <input type="text" placeholder="Enter name"
class="form-control" required ng-model="user.mail">
        </div>
        <div class="form-group">
            <label for="exampleInputPassword">Password</label>
            <input type="password" placeholder="Password"
id="exampleInputPassword" class="form-control" required
ng-model="user.pass">
        </div>
        <button class="btn btn-default" type="submit"
ng-disabled="form1.$invalid" ng-click="login(user)">Submit</button>
        <p></p>
    </form>
</div>
abu abu
  • 6,599
  • 19
  • 74
  • 131

1 Answers1

0

You need something like this in PHP:

Helper:

function getReturnObject ($message, $code) {
    $response = new StdClass();
    $response->message = $message;
    $response->code = $code;
    return $response;
}

Main code:

print json_encode(getReturnObject('auth is successull', 200);

Or:

header('HTTP/1.0 403 Forbidden');
print json_encode(getReturnObject('auth is bad', 403);

Then in JavaScript you need this:

var module = angular.module('myApp', []);
module.controller('myCtrl', ['$http', function ($http) {
  var user = null; // some user
  $http.post('data/user.php', user)
    .success(function (data) {
    // handle response there
    })
    .error(function (data) {
      console.log('error');
    });
}]);
Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
  • Thanks @Sharikov Vladislav for your reply. I used `$user=json_decode(file_get_contents('php://input'));`. Should I use `$user=json_encode(file_get_contents('php://input'));` ?? How can I debug PHP file ?? Thanks – abu abu Nov 24 '15 at 06:43
  • Look at [this](http://stackoverflow.com/questions/18866571/receive-json-post-with-php) post – Sharikov Vladislav Nov 24 '15 at 07:45