0

I have an issue in angularjs form, i can't manage to get the data from the form (ng-model) with php.

<div class="divProfil">
      <form id="formProfil" ng-submit="submit()">
        <p><span>Bonjour  </span><input  type="text" name="loginProfil" ng-model="loginProfil"/></p>
        <p>Mon mot de passe:  <input  type="text" name="mdpProfil" ng-model="mdpProfil"/></p>
        <p> Email:  <input  type="email" name="emailProfil" ng-model="emailProfil"/></p>
        <input class="btn btn-primary" type="submit" value="Enregistrer"/>
      </form>

  </div>

I submit the form with $http which work fine but when i try to do in php :

$login = $_POST['loginProfil'];

$mdp = $_POST['mdpProfil'];

$email = $_POST['emailProfil'];

each of this variable is empty. Need some help, please!

2
  • 1
    Please show your angularjs code, what you have tried? Commented Jan 24, 2018 at 13:02
  • you would post it with $scope.submit=function(){$http.post('post.php',data);} and receive in PHP with $r=json_decode(file_get_contents("php://input"));, $email = $r->email; Commented Jan 24, 2018 at 13:16

1 Answer 1

1

I think it might be a issue with the serialization of the data you send, especially if you use the $http.post() function.

If you use $http.post() and then on the PHP side do like this:

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;

Else you need to change how the data is sent on the Angular side by using $.param() function.

$http({
    method  : 'POST',
    url     : '<PATH_TO_END_POINT_HERE>.php',
    data    : {$.param($scope.formData),  // pass in data as strings}
    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
})
.success(function(data) {
    console.log(data);
});

Then you should be able to use your code on the PHP side.

$email = $_POST['email'];

You can read more about it here:

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

2 Comments

thank you for the answer, can you explain to me what does "php://input" means?
Look at this answer here for more info: stackoverflow.com/questions/2731297/…, or here php.net/manual/en/wrappers.php.php

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.