1

I am trying to get the properties of an array object and save it into the DB, here is my POST method

$http({
     method:"POST",
     async: false,
     data: {first_name:'dilip',last_name:'belgumpi'}, url:"insert.php"
});

and in insert.php

$first_name=$_POST["first_name"];

When I run this code I am getting error like this

<b>Notice</b>:  Undefined index: first_name in <b>C:\xampp\htdocs\tutor_crm\insert.php</b> on line <b>10</b><br />
Dilip Belgumpi
  • 658
  • 4
  • 13

1 Answers1

1

As you sending request through angular, need to set headers in your request.

$scope.add = function() {
var FormData = {
  'first_name' : 'dilip',
  'last_name' : 'belgumpi'
};
$http({
     method:"post",
     data: $.param({'data' : FormData}),
     headers: {'Content-Type': 'application/x-www-form-urlencoded'},
     url:"insert.php"
});

File: insert.php

<?php
       $data = $_POST['data'];
       var_dump($data);
?>
Naincy
  • 2,953
  • 1
  • 12
  • 21
  • For other way Refer this https://codeforgeek.com/2014/07/angular-post-request-php/ – Naincy Feb 28 '17 at 10:32
  • It's working!! Thank you so much PS: I tried Content-Type : application/json but was not working.. I just replaced with yours now its working fine. Thank u! – Dilip Belgumpi Feb 28 '17 at 10:33