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!

IZAD
  • 7
  • 5
  • 1
    Please show your angularjs code, what you have tried? – Kirit Jan 24 '18 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;` – Aleksey Solovey Jan 24 '18 at 13:16

1 Answers1

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:

KungWaz
  • 1,918
  • 3
  • 36
  • 61
  • thank you for the answer, can you explain to me what does "php://input" means? – IZAD Jan 25 '18 at 16:42
  • Look at this answer here for more info: https://stackoverflow.com/questions/2731297/file-get-contentsphp-input-or-http-raw-post-data-which-one-is-better-to, or here http://php.net/manual/en/wrappers.php.php – KungWaz Jan 26 '18 at 09:54