2

I have a simple form with two fields.

<form name="save" ng-submit="sap.saved(save.$valid)" novalidate>

        <div class="form-group" >
            <input type="text" name="name" id="name" ng-model="sap.name" />
        </div>

        <div class="form-group" >
            <input type="email" name="email" id="email" ng-model="sap.email" />
        </div>

        <div class="form-actions">
            <button type="submit" ng-disabled="save.$invalid || sap.dataLoading">Save</button>
        </div>
</form>

I'm trying to post these values to a php page using POST request.

                $http({
                  method: 'post',
                  url: 'save.php',
                  data: $scope.vm,
                  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                })
                .success(function(data, status, headers, config) 
                {
                    if(data.success)
                    {
                        alert('valid');
                    }
                    else
                    {
                        alert('invalid 1');
                    }
                }).
                error(function(data, status, headers, config) 
                {
                    alert('invalid 2');
                });

And in save.php file, I'm fetching the values like this:

$name=$_POST['name'];
$email=$_POST['email'];

But response I get is undefned variable name. That's a PHP notice. But when I try to POST these values from POSTman client, it works. Is there anything wrong with the method I choose to POST data?

P.S: i didn't post entire controller. Just http request part is included here.

version 2
  • 1,049
  • 3
  • 15
  • 36

1 Answers1

1

I faced same situation where $_POST() did't worked out!

trying to fetch raw data with:

json_decode(file_get_contents('php://input'))

might be helpful.

Update:

This Answer explains why $_POST variable is not populated in php when data is posted with angular!

Community
  • 1
  • 1
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69