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.