2

I am new with Anggularjs.I have a form and send ng model array to php file,i am getting error undefined index,don't know how to parse sent array from angularjs in php.

Part of html form

                   <div class="form-group" >
                        <label>Street<span class="error">*</span></label><span ng-show="!userForm.street.$pristine && userForm.street.$invalid || (userForm.street.$touched && !userForm.street)"  class="error">Must be a number</span>
                        <input type="text" id="strret"  class="form-control" name="userForm.street" ng-model="userForm.street" required ng-value="userForm.street" />
                    </div>

                    <div class="form-group" >
                        <label>Tel<span class="error">*</span></label><span ng-show="!userForm.tel.$pristine && userForm.tel.$invalid || (userForm.tel.$touched && !userForm.tel)"  class="error">Must be number</span>
                        <input type="text" class="form-control" name="userForm.tel" ng-model="userForm.tel" required   ng-pattern="/^[0]{1,13}$/" ng-value="userForm.tel"/>
                    </div>

Angularjs post function inside controller:

$http({
    method  :'POST',
    url:'post.php',

    data: $scope.userForm,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}

})
.success(function(data){
alert(data);
    }).error(function(err) {
           alert(err);
    });

My problem is how to take userForm.street and userForm.tel in PHP $_POST[]...There are similar questions on stackoverflow,but there is no any example of how to do this in php file.

2
  • 1
    You might wanna take a look at this question as I think it adresses concerns similar to yours : stackoverflow.com/questions/15485354/… I have been in the same situation as you and I ended up using JSON because it is much simpler with angular. The question above even has a nice example on how to retrieve the JSON on the PHP side. Commented Mar 16, 2016 at 11:28
  • Thanks for answer,I am reading this if I find solution I will post it. Commented Mar 16, 2016 at 11:34

1 Answer 1

1

In order to send data in form encoded format you need to serialize it as such.

The internal defaults are to serialize it to JSON

Use $httpParamSerializerJQLike service.

$http({
    method  :'POST',
    url:'post.php',

    data: $httpParamSerializerJQLike($scope.userForm),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}

}).success...

Don't forget to inject in service or controller where it will be used

Otherwise if using $http defaults which send data as application/json you need to access data using file_get_contents('php://input') as `$_POST will be empty

$data = json_decode(file_get_contents('php://input'));
Sign up to request clarification or add additional context in comments.

6 Comments

Still no luck,getting error undefined index street...i know how to send data but i don't know how my php should look like if i want to take values of sent array.....i've tried everything above
Need to show the php code as well as identify which of the above solutions you used
Error: $httpParamSerializerJQLike is not defined getting this error too.
Because you didn't inject it as a dependency in component you are using it. See comment under the code above
here is my controller angular.module("app", []) .controller("ctrl", ['$scope', '$http' , function($scope, $http, $httpParamSerializerJQLike) { }]); I dont know how to set $httpParamSerializerJQLike...it shows this mistake Error: $httpParamSerializerJQLike is not a function
|

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.