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.

1 Answer 1

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!

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

Comments

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.