1

config.service.ts file ->

 constructor (private http:Http){
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        this.options = new RequestOptions({ headers: headers });    
        this.API_BASE_URL='http://localhost/confalerts/api/savetodb.php';

     }

saveConference(data){
     return this.http.post(this.API_BASE_URL,data,this.options).map((res)=>res.json(),(err)=>err);
 }

Service call in component.ts ->

  let data=this.newconfForm.value;
      let pair={action_type:"saveconference"};
      data={...data,...pair};   
        this.configSvc.saveConference(data).subscribe((jsondata)=>{
          console.log('successfully called');
      },(err)=>{

      });

PHP File ->

`header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
$postData=json_decode($_POST['data'],true);
print_r($postData);`

My problem is-> PHP file throws the NOTICE:

Notice: Undefined index: Formdata in
C:\xampp\htdocs\confalerts\api\savetodb.php on line 8

$postData=json_decode(file_get_contents('php://input'),true); 

is an easy hack but i'll be uploading files in this form as well so I dont wish to use the php://input file.

What could be a possible solution?
I tried appending headers, but header.append() is apparently not a supported function.

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
Div
  • 63
  • 2
  • 10
  • Change your Angular Header also to `'Content-Type: application/json; charset=utf-8'` and then do a `print_r($_POST)` and take a look at whats coming in. – JohnnyDevNull Sep 26 '18 at 13:20
  • I am a beginner, what will that become? let headers = new Headers({ 'Content-Type: application/json; charset=utf-8' }); -> this is giving an error, you gotta share exact syntax please – Div Sep 26 '18 at 13:24
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Sep 26 '18 at 13:50
  • Your code doesn't include "Formdata" anywhere, so this is not what's causing the error. Regardless, this is a duplicate. – miken32 Sep 26 '18 at 13:52
  • you have posted your code, replace your formdata header: `let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' })` – JohnnyDevNull Sep 26 '18 at 14:25
  • @JohnnyDevNull , your idea worked, my data is going to the PHP file now, but it is filled with underscores.. Array([{ _"title":_"hello" }]) something like this, should i ask a new question, or something... – Div Sep 27 '18 at 09:20
  • Edit your post with a example response, then i can help you. – JohnnyDevNull Sep 27 '18 at 13:19

0 Answers0