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);
}
4
  • 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. Commented Oct 10, 2015 at 20:10
  • No error, it's just not picking up that a POST variable is passed. Path is correct. Commented Oct 10, 2015 at 20:19
  • @buzzsaw directory or path of js file has no relevance at all Commented Oct 10, 2015 at 20:23
  • 2
    $http sends data as json by default...leaves `$_POST empty. See stackoverflow.com/questions/15485354/… Commented Oct 10, 2015 at 20:26

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I'm used to making AJAX calls from jQuery. Thanks!

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.