1

I'm making a http request where i need to send some values with it.

Http request

$http({
           url: 'user_resources/delete.php',
           method: 'POST',
           data: { parent_state: parent_state, course_id: course_id, filename: material_to_delete.material.file_name }
    });

It runs the php file but the $_POST values aren't there. I've also tried to include headers: {'Content-Type': 'application/x-www-form-urlencoded'} in my http request but without any luck. Can somebody help me figure out what I'm doing wrong?

Thank you in advance!

Update

Controller

app.controller('CourseEditController', ['$http', '$scope','$sessionStorage','$state','$log','Session','api','$filter', function ($http, $scope, $sessionStorage, $state, $log, Session, api, $filter) {
    var course_id = $state.params.course_id;
    var parent_state = $state.$current.parent.name;

    $scope.deleteMaterial = function (idx) {
        var material_to_delete = $scope.course.course_has_materials[idx];
        $http({
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            url: 'user_resources/delete.php',
            method: 'POST',
            data: { parent_state: parent_state, course_id: course_id, filename: material_to_delete.material.file_name }
        });
    };
}]);

HTML

<table class="table table-striped b-t b-light" ng-controller="CourseEditController">
   ...
   <tbody>
      <tr ng-repeat="material in course.course_has_materials">
          <td class="l-h-2-5x">{{ material.material.file_name }}</td>
          <td class="l-h-2-5x">{{ material.material.file_type }}</td>
          <td class="l-h-2-5x text-right pointer">
             <i class="fa fa-times" ng-click="deleteMaterial($index)"></i></td>
      </tr>
   </tbody>
</table>
Backer
  • 1,094
  • 1
  • 20
  • 33
  • I'm assuming the variables you post actually exist and are not null at the time of post? Also, try var_dumping POST – Huey Apr 16 '15 at 13:08
  • 1
    Check the value of fopen("php://input", "r") and see if the data is there – cwurtz Apr 16 '15 at 13:11
  • @Huey They all have values yes, and none of them arre null – Backer Apr 16 '15 at 13:11
  • @CJ Wurtz I just tried that and this is what i get back `result={resource}resource id='3' type='stream'` – Backer Apr 16 '15 at 13:13
  • 1
    Sorry, I should have been more specific. fopen() will give you the resource, you need to check the value of 'fread(fopen("php://input", "r"));' – cwurtz Apr 16 '15 at 13:17
  • So...the title says "angular" in it. Can you post some of your html and controller code too? I'd venture a guess it's a scoping issue if your server side data looks like this: { parent_state: null, course_id: null, ...etc } – wholevinski Apr 16 '15 at 13:17
  • @Alexander As i mention in my post I've already tried that, but with no luck – Backer Apr 16 '15 at 13:17
  • 1
    http://stackoverflow.com/questions/11442632/how-can-i-post-data-as-form-data-instead-of-a-request-payload/11443066#11443066 – epascarello Apr 16 '15 at 13:18
  • @dubhov I have just updated my post. Tell me if you need more of my code. @CJ Wurtz Also tried that and it returns `false` – Backer Apr 16 '15 at 13:37

1 Answers1

0

Looking at the post @epascarello posted help me resolve my issue.

$http({
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      url: 'user_resources/delete.php',
      method: 'POST',
      data: $.param({parent_state: parent_state, course_id: course_id, filename: material_to_delete.material.file_name})
 });

If anybody in the future should come out for the same issue.

Backer
  • 1,094
  • 1
  • 20
  • 33