0

I have looked at other options for posting data to php using angular but haven't had any success. I have an object called "user" that I have logged in js and can see that it's being populated. I am doing the post like

$http.post("./api/register.php", user, config).then(function(response) {
                return response.data;
            });

with a config object that looks like

var config = {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }

I have done an echo of a "hello" string in the php and I am able to see that however when I try to echo one of my variables I am unable to. Right now my php looks like

<?php

  $postdata = file_get_contents("php://input");
  $request = json_decode($postdata);
  $email = $request->email;
  $pass = $request->firstName;
  echo $email;
?>

I have lots of angular experience however only using java spring.

ryandonohue
  • 189
  • 4
  • 22
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception or error, post the line it occurred on and the details. Please [edit] these details in or we may not be able to help. – Mr. Llama Jun 19 '16 at 23:59
  • Sorry, when I echo a "hello" string I am able to see it. When I echo my email variable it prints an empty line. I'm wondering why my $postdata object isn't being filled. – ryandonohue Jun 20 '16 at 00:01
  • can You just do var_dump($_REQUEST); above $postdata and put result in question? also open inspector panel of chrome and go to network tab to see what returns. – num8er Jun 20 '16 at 00:13
  • Possible duplicate of [Angular HTTP post to PHP and undefined](http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined) – num8er Jun 20 '16 at 00:20
  • Yes I used that as an example. However I can't get the solution to work for me. – ryandonohue Jun 20 '16 at 00:22
  • @num8er that is deprecated and won't solve anything anyway Also if you use that header have to redo the php and paramterize the data in the post – charlietfl Jun 20 '16 at 00:25
  • @ryandonohue how about using minimal php framework that has features to unify body handling? for example: http://docs.slimframework.com/request/body/ – num8er Jun 20 '16 at 00:48

3 Answers3

2

You are mixing approaches for application/json and application/x-www-form-urlencoded.

Since default for $http is to send as json and the php is using file_get_contents("php://input") I suggest you simply remove the config from the request

$http.post("./api/register.php", user).then(fun....
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Okay I did that, but still don't get anything in my php object. – ryandonohue Jun 20 '16 at 00:14
  • Is request succeeding? You have no error handling. Can also inspect actual request in browser dev tools network for clues. Also not returning json response which is `$http` default `dataType` expected – charlietfl Jun 20 '16 at 00:16
  • Yes the request is succeeding. I uploaded some screenshots here http://imgur.com/a/MikYC The first image has a console log of the object I'm sending and the second log in what I'm getting as a response. The second image is my network request. The third image is when I replace the echo $email with an echo "hello" – ryandonohue Jun 20 '16 at 00:20
  • so do a dump of `$postdata` and see what it sends back – charlietfl Jun 20 '16 at 00:25
  • when I dump `$postdata` I get `string(0) ""` – ryandonohue Jun 20 '16 at 00:27
  • @ryandonohue You'll get empty string from input stream, because Your data is send as post body (You've changed enctype, so web server tries to decode input as serialized=atrributes&like=this – num8er Jun 20 '16 at 00:45
  • @num8er that is not true if remove the config header and let the request go with default `application/json`. I have done this both ways in php hundreds of times – charlietfl Jun 20 '16 at 00:48
  • @charlietfl in Your answer You suggest to remove config param. It's same thing as I say not to change content type. Why You downvoted my answer? – num8er Jun 20 '16 at 01:04
  • 1
    @num8er you are on the 5th revision of your answer and at first you were mixing approaches up just as bad as OP was – charlietfl Jun 20 '16 at 01:06
  • @charlietfl I do not downvote anybody, we are not fighting, it's not the way how professional acts. You said Your thoughts and I agreed. You're stack points collector? So I upvoted You. – num8er Jun 20 '16 at 01:09
  • No..I lose points for downvote. Answer was clearly off track. That's what votes are for. i removed it but if it wasn't there chances are there wouldn't have been 4 more revisions – charlietfl Jun 20 '16 at 01:12
  • OK @charlietfl I hate "virtual code wars", when I waste time and energy for nothing. Anyway thank You for pointing to my failures in code. I prefer NodeJS than PHP. So I never have such issues on my express app. I'll have some sleep, it's to late for my timezone. – num8er Jun 20 '16 at 01:22
0

I would recommend to not to change headers, keep it as default (application/json):

$http({ 
  method : 'POST', 
  url : '/api/register.php', 
  data: user
})
.then(
  function(res) {
    console.log('SUCCESS', res);
  },
  function(err) {
    console.error('ERROR', err);
  });

this php code have to work if You send json object in input:

<?php

  $postdata = file_get_contents("php://input");
  $request = json_decode($postdata);
  $email = $request->email;
  $pass = $request->firstName;
  echo $email;

or how about write normal minimal Slim Framework app:

$app = new \Slim\Slim();
$app->post('/api/register', function () use ($app) {
    $body = $app->request->getBody();
    var_dump($body);
});
$app->run();

most of modern frameworks have great features to unify request handling.

when I've to do simple api on php I use Slim. here You can see quick start, and make Your own PHP API

num8er
  • 18,604
  • 3
  • 43
  • 57
  • 1
    This is simply not how `'application/x-www-form-urlencoded'` works on either end. You would use `$_POST` in php and also have to serialize the data in `$http` – charlietfl Jun 20 '16 at 00:30
  • @charlietfl I know! I want him to do some debugging, to get some data and put in answer, $_REQUEST array keeps GET, POST array. In another hand data param of $http can be like 'username=blabla&password=blapass' – num8er Jun 20 '16 at 00:34
  • But you aren't helping by mixing the 2 Content types up yourself. It has to be one type or the other and consistent on both ends. Your answer just adds to confusion since it would never work as shown – charlietfl Jun 20 '16 at 00:35
  • @charlietfl it's debugging, I wanna see request reaches to code or not! I know that in normal case it will be in $_POST array. – num8er Jun 20 '16 at 00:38
  • Only if you serialize the data properly...which you are not – charlietfl Jun 20 '16 at 00:38
  • Actually I had such issue, I solved it by not changing headers in angular, it was application/json, in my Phalcon app I have getJsonRawBody function of Request object, it automatically converted json input to body object and injected it as component – num8er Jun 20 '16 at 00:40
  • There is a serializer built into `$http` called `$httpParamSerializerJQLike` that will serialize for form encoding the same way `$.param` does in jQuery ajax. But again both ends should be consistent – charlietfl Jun 20 '16 at 00:46
0

var_dump($postdata); whole data from server end, you can find whether data will reach server side or not. If data will not reach to back end, from front end use console.log(data)->check it have json data. please follow below approch to send a data from front end.

Angular Code

$http({ 
  method : 'POST/GET', 
  url : '/api/register.php', 
  parm: user   // this hold an data
})
.then(
  function(response) {  
    console.log(response);
  },
  function(erroMessage) {
    console.error(erroMessage);
  });
Anand Mohan
  • 79
  • 15