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.

7
  • 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. Commented Jun 19, 2016 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. Commented Jun 20, 2016 at 0: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. Commented Jun 20, 2016 at 0:13
  • Possible duplicate of Angular HTTP post to PHP and undefined Commented Jun 20, 2016 at 0:20
  • Yes I used that as an example. However I can't get the solution to work for me. Commented Jun 20, 2016 at 0:22

3 Answers 3

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

12 Comments

Okay I did that, but still don't get anything in my php object.
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
Yes the request is succeeding. I uploaded some screenshots here 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"
so do a dump of $postdata and see what it sends back
@num8er you are on the 5th revision of your answer and at first you were mixing approaches up just as bad as OP was
|
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

7 Comments

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 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'
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 it's debugging, I wanna see request reaches to code or not! I know that in normal case it will be in $_POST array.
Only if you serialize the data properly...which you are not
|
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);
  });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.