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.

TaZz
  • 652
  • 7
  • 20
Tim
  • 65
  • 1
  • 11
  • 1
    You might wanna take a look at this question as I think it adresses concerns similar to yours : http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined 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. – kerwan Mar 16 '16 at 11:28
  • Thanks for answer,I am reading this if I find solution I will post it. – Tim Mar 16 '16 at 11:34

1 Answers1

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'));
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • 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 – Tim Mar 16 '16 at 14:58
  • Need to show the php code as well as identify which of the above solutions you used – charlietfl Mar 16 '16 at 14:58
  • Error: $httpParamSerializerJQLike is not defined getting this error too. – Tim Mar 16 '16 at 15:04
  • Because you didn't inject it as a dependency in component you are using it. See comment under the code above – charlietfl Mar 16 '16 at 15:06
  • 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 – Tim Mar 16 '16 at 15:35
  • update `angular.module("app", []) .controller("ctrl", ['$scope', '$http', '$httpParamSerializerJQLike' , function($scope, $http, $httpParamSerializerJQLike) { }]);` – dipesh May 11 '16 at 20:41