dream04110
2017-01-17 04:13
浏览 49
已采纳

angular-php:表单数据在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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpiao9078 2017-01-17 04:46
    已采纳

    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.

    点赞 打赏 评论
  • douyoufan7881 2017-01-17 04:29

    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'];
    
    点赞 打赏 评论

相关推荐 更多相似问题