简体   繁体   中英

angular-php: form data is empty in php

I am using angular as frontend and php as backend here is the code of angular.

$scope.processForm = function($scope.formData) {
    console.log($scope.formData);
    $http({
        method  : 'POST',
        url     : 'process.php',
        data    : {'data': $scope.formData,'uid':uid}, // no need to use $.param, even never see it in angular
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  
    })

here is the process.php

$postContent= file_get_contents("php://input"); 
$req= json_decode($postContent); 
$formData= $req->formData; 
$uid= $req->uid;

problem is $formData is empty in php. however $uid is showing value.

in form i have two entry email and password but i don't know how can i use that in php because formdata is empty.

I checked in firebug and found data is posting.

{"formData":{"password":"ff","cpassword":"errgreg"},"uid":"75"}:""

But nothing is coming response tab of firebug.

Assuming you call your function with something like ng-submit="processForm(formData)" then this is all you actually need

$scope.processForm = function(formData) {
  $http.post('process.php', {
    formData: formData, // note the key is "formData", not "data"
    uid: uid // no idea where uid comes from
  });
};

Where you have

$scope.processForm = function($scope.formData) {

isn't even valid JavaScript. You cannot use object dot notation in function argument names. That should have been throwing an error in your console.


You also appeared to be incorrectly setting your request content-type. You are sending JSON, not application/x-www-form-urlencoded formatted data. Angular's default POST content-type ( application/json ) is sufficient.

Try like this..

$json='{"formData":{"password":"ff","cpassword":"errgreg"},"uid":"75"}';
$req= json_decode($json); 
$formData= $req->formData; 
$uid= $req->uid;
$password = $req->formData->password;
$cpassword = $req->formData->cpassword;

OR convert into array using json_decode() with second argument as true .

$json='{"formData":{"password":"ff","cpassword":"errgreg"},"uid":"75"}';
$req= json_decode($json,true);//converts into array format 
$formData= $req['formData']; 
//print_r($formData);
echo $formData['password'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM