1

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';

?>
2
  • If you inspect 'msg' in the $promise.then function, what do you see? Commented Dec 16, 2015 at 16:29
  • i consle.log(msg) and i get undefined Commented Dec 16, 2015 at 16:35

1 Answer 1

2

pass the user in your ng-click function

ng-click = "login(user)"

Sign up to request clarification or add additional context in comments.

2 Comments

Ye now console logs correctly but i still get error as print return.
Finaly i got it. Thanks mate a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.