0

I am having a problem using post method in Angular 5 and PHP.

I have this method from a .ts class:

addPartners(partnerName)
{
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    this.name = JSON.stringify(partnerName)
    console.log("hi "+ this.name)
    return this.http.post('http://aff.local/addPartner.php', this.name, {
      observe: 'response',
      responseType: 'json'
    }).pipe(map(
    res=>{
      console.log(res)
    }
  ))
}

And I will call it on (click) event of a button:

addPartner(){
    this.email = this.subscribeForm.get('emailTxt').value;
    //console.log(this.email)
    this.api.addPartners(this.email).subscribe(
      (data)=>{
        console.log(data);
        this.subscribeForm.reset();
      },
      (error)=>{
        console.log(error)
      }
      );
}

The PHP script is :

addPartner($partnerName); echo $result; ?>

When I fill the textbox and click on the button, the value sent is empty.

When I change the method, by sending the variable in the url it work properly.

Here is the working script. In the api main class:

addPartners(partnerName)
  {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    this.name = JSON.stringify(partnerName)
    console.log("hi "+ name)
    return this.http.post('http://aff.local/addPartner.php?name='+ name, {
      observe: 'response',
      responseType: 'json'
    }).pipe(map(
        res=>{
          console.log(res)
        }
      ))
  }

I just changed the url into:

http://aff.local/addPartner.php?name='+ name,

And in the php script I will get it using $_REQUEST['name'].

What I want is using the POST method because I need to send multiple data from a form.

6
  • There is your problem ? Multiple parameters can be send like this angular.io/guide/http#url-parameters . Or does it fal in your php script ? If yes please add it. Commented Jan 22, 2018 at 13:00
  • remove ?name=name from the url and add it as post param .. ?name=name is regular GET param and it's not send as POST Commented Jan 22, 2018 at 13:03
  • I don't want to send a variable through URL. I need to send multiple variables using POST method Commented Jan 22, 2018 at 13:04
  • On http.post 2nd param after url is the array with DATA to be posted .. Whatever you want to receive at the PHP file as POST data must be attached there. . Commented Jan 22, 2018 at 13:06
  • I can see the data at the console. So there is no problem from angular side. Commented Jan 22, 2018 at 13:07

2 Answers 2

3

if you want to use JSON.stringify so you can take params from the server side using :

$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

or if we want to just take data using $_POST['paramName'] so we have to use the below in your client side:

let headerOptions = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
let data = {name: partnerName};
let body = this.formatData(test);

and format your data like that:

formatData(data) {
    let returnData = '';
    let count = 0;
    for (let i in data) {
      if (count == 0) {
        returnData += i + '=' + data[i];
      } else {
        returnData += '&' + i + '=' + data[i];
      }
      count = count + 1;
    }
    return returnData;
  }
Sign up to request clarification or add additional context in comments.

Comments

0

In the POST-example you used this.name, but in the GET-example only name.

Try to use $_POST['name'] instead of $_REQUEST['name], because $_REQUEST['name] is a different variable then $_POST or $_GET and also differently treated.

Comments

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.