0

Very new with AngularJS, I'm sure it's something simple I'm doing wrong but I cannot figure it out. All I'm trying to do is log the incoming POST from Angular, using PHP.

AngularJS function:

$scope.addTask = function() {
        $http.post('process.php', { newTask: $scope.newTask });
        $scope.newTask = '';
    };

process.php:

if (isset($_POST['newTask'])) {
        createLog('test');
    }

function createLog ($str) {
    $file = 'log.txt';
    $str .= "\n";
    file_put_contents($file, $str, FILE_APPEND | LOCK_EX);
}
Tim Aych
  • 1,365
  • 4
  • 16
  • 34
  • What error are you getting? Is your process.php file in the same directory as your .js file? If not, it is a relative path issue. – buzzsaw Oct 10 '15 at 20:10
  • No error, it's just not picking up that a POST variable is passed. Path is correct. – Tim Aych Oct 10 '15 at 20:19
  • @buzzsaw directory or path of js file has no relevance at all – charlietfl Oct 10 '15 at 20:23
  • 2
    `$http` sends data as json by default...leaves `$_POST empty. See http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined – charlietfl Oct 10 '15 at 20:26

1 Answers1

1

If you are sending in a POST to PHP from angular, you need to retrieve it a bit differently in your backend. As noted by @charlietfl, the $_POST is empty. I have been out of the PHP game for a while but this should work for you.

$data = file_get_contents("php://input");
$request = json_decode($data);
buzzsaw
  • 1,476
  • 1
  • 10
  • 14