0

I have a factory service in Angular that is successfully posting my email in the HTTP request which I have verified with Firebug, but the email is not being seen by my PHP file. I am reaching my PHP file since I can echo a response from it, but the email value I sent it is not being received as I have already mentioned.

Controller (partial):

$scope.subscribe = function() {
        if($scope.email.length > 0) {
            Subscribe.save({},$scope.email);
        } else {
            alert("Please enter your email address");
        }
    }

Factory:

app.factory('Subscribe', ['$resource', function($resource) {
    return $resource( 'server/restdb.php:email', 
        {email: '@email'},
        {

        }
    ); 
}]);

PHP:

<?php
echo 'email: '.$_REQUEST['email'];

I have also tried using $_POST to get the email as well.

So to recap:

  • Posts email in HTTP request successfully.
  • PHP file is being accessed and getting response from it.
  • PHP not receiving the email value I sent it.
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
appthat
  • 816
  • 2
  • 10
  • 30

2 Answers2

1

Try to use this:

$aRequest = json_decode(file_get_contents("php://input"), true);
echo 'email: ' . $aRequest['email'];
Pataar
  • 651
  • 8
  • 14
  • 1
    Thank you this works! Also this answers my question that i asked here http://stackoverflow.com/questions/41193281/how-to-use-angularjs-resource-with-php-script – Sahbaz Dec 17 '16 at 00:13
0

Oh, $resource.save is a bit "weird".

you should use it this way :

var myData = new Subscribe({email: $scope.email});
myData.save();

and you can simplify the factory to something like :

app.factory('Subscribe', ['$resource', function($resource) {
    return $resource( 'server/restdb.php', {}, {}
    ); 
}]);

Look a bit more at the doc : angular.js $resources