2

I have a function inside an AngularJS controller that let me send POST data to a PHP page (it should be for authentication)

$scope.submit = function () {
    $http.post(api + 'login.php', $scope.user)
};

This function is called from this HTML code

<div>
    <span ng-show="isAuthenticated">{{welcome}}</span>
    <form ng-show="!isAuthenticated" ng-submit="submit()">
        <input ng-model="user.username" type="text" name="user" placeholder="Username" />
        <input ng-model="user.password" type="password" name="pass" placeholder="Password" />
        <input type="submit" value="Login" />
    </form>
    <div>{{error}}</div>
    <div ng-show="isAuthenticated">
        <a ng-click="logout()" href="">Logout</a>
    </div>
</div>

Using Firebug inside Firefox I can see that auth page is called and POST data is sent correctly

enter image description here

Problem raises within my auth PHP code: I try to retrieve POST data, but array is empty.
I've also tried to write content in a file and I can confirm array is empty (file contains [])

$userdata = $_POST;
file_put_contents("login.txt",json_encode($userdata)."\n", FILE_APPEND);

What's wrong with my code? I've also tried changing from POST to GET (using $http.get), but $_GET array is empty too...

4
  • Can you post the output of var_dump($_POST)? Commented Feb 12, 2015 at 16:55
  • It's what I wrote in the file: it's [ ] Commented Feb 12, 2015 at 16:56
  • can you then post var_dump(file_get_contents("php://input"))? Commented Feb 12, 2015 at 16:57
  • or var_dump(http_get_request_body()? Commented Feb 12, 2015 at 16:59

1 Answer 1

8

Angular sends data over as json, not as form data, to retrieve it you can do something like:

$_JSON = json_decode(file_get_contents("php://input"), true);
Sign up to request clarification or add additional context in comments.

2 Comments

Never in my life I could have imagined this... I've lost hours for this. Thank you so much!
Never in my life would I have imagined this either. Good to know for future projects as angular is about to be added to the company library list. You may have just saved me hours.

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.