Reputation: 113
I'm writing a simple login service, I'm just trying to make username and password, send them to php and get simple console.log 'GJ'. Everything works fine until i get response 'Error' from my PHP function. I tryed to console.log data I'm sending and it says undefined. I tryed to stringify with JSON same thing. Can anyone check where i have it wrong
HTML
<div class="container login">
<div class="column column-4">
<form name="from1" role="form">
<label for="">{{user.mail}}</label> <br>
<input type="text" placeholder="username" required ng-
model="user.mail"><br>
<label for="">{{user.pass}}</label> <br>
<input type="text" placeholder="password" required ng-
model="user.pass"><br>
<button name="submit" class="btn btn-default" ng-click="login()">
Submit</button>
</form>
</div>
</div>
MAIN.JS
app.factory('loginService',function($http){
return{
login:function(user,scope){
var $promise = $http.post('http://site1.local/user.php', user);
//send data to user.php
$promise.then(function(msg){
if(msg.data=='success') console.log("GJ");
else console.log("DARN IT");
});
}
}
});
app.controller('LoginCtrl', function( $scope, loginService ) {
$scope.login=function(user){
console.log(user);
loginService.login(user,$scope); //call login service
};
});
PHP
<?php
$data = file_get_contents("php://input");
$user = json_decode($data);
if($user->mail=='admin' && $user->pass=='1234')
print 'success';
else
print 'error';
?>
Upvotes: 1
Views: 58
Reputation: 940
pass the user in your ng-click function
ng-click = "login(user)"
Upvotes: 2