0

I am using Angular to send some json data to server. It gives me the entire php file as data in return. On my bootstap modal I have ng-submit=createNewProgram()

$scope.createNewProgram = function()
{
    var data = {
        'programName': $scope.programName,
        'startDate' : $scope.startDate,
        'endDate' : $scope.endDate,
        'startTime': $scope.startTime,
        'endTime' :$scope.endTime
    };`


    $http({
      method  : 'POST',
      url     : 'curl.php',
      data    : data, //forms user object
      headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })
    .success(function(data) {
        if (data.errors) {
          // Showing errors.
          $scope.message = 'wrong data';
        } else {
          $scope.message = data;
        }
    });

}

This is how I am recieving data through my php file (curl.php):

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$programName = $request->programName;
@$startDate = $request->startDate;
echo $programName;
jbafford
  • 5,528
  • 1
  • 24
  • 37
Nitish Hardeniya
  • 181
  • 2
  • 15

3 Answers3

0

You are sending POST data as content-type.

Use application/json to get json from http call.

Reading JSON POST using PHP

Community
  • 1
  • 1
Yuri Blanc
  • 636
  • 10
  • 24
  • tried changing content type to `application/json` but still getting same result – Nitish Hardeniya Jan 14 '16 at 13:13
  • try doing this in angular data: $.param({'data' : FormData}), using application/x-www-form-urlencoded. Then access the variable $_POST['data'] and then use json_decode. – Yuri Blanc Jan 14 '16 at 13:44
0

Actually your writing code has some problem.You include a " ` " before $http part.

var data = {
        'programName': $scope.programName,
        'startDate' : $scope.startDate,
        'endDate' : $scope.endDate,
        'startTime': $scope.startTime,
        'endTime' :$scope.endTime
    };`

You do not need to write this code in curl.php file .You can get value of programName parameter using $_REQUEST super global variable.

$scope.createNewProgram = function()
{

   var data = {
            'programName': $scope.programName,
            'startDate' : $scope.startDate,
            'endDate' : $scope.endDate,
            'startTime': $scope.startTime,
            'endTime' :$scope.endTime
        };

     $http({
        method: "POST",
        url: "curl.php",
        params: data,
        headers:{
         'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(function(response) { // Success time
         $scope.message = response.data;
       }, function(response) { // Faulure time
         $scope.message = 'wrong data';
      });

}

The above mentioned is the correct angular js code for your mentioned code.

If you want to get "programName" parameter value,write php code like that

<?php

if( isset($_REQUEST["programName"]) === true ) {
  echo $_REQUEST["programName"];
} else {
  echo "";
}

?>
0

Edit - JS Code:

$scope.createNewProgram = function()
{
    var data = {
        'programName': $scope.programName,
        'startDate' : $scope.startDate,
        'endDate' : $scope.endDate,
        'startTime': $scope.startTime,
        'endTime' :$scope.endTime
    };


    $http({
      method  : 'POST',
      url     : 'curl.php',
      data    : $.param(data), /*forms user object*/
      headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })
    .success(function(data) {
        $scope.message = data;
    })
    .error(function(error) {
        $scope.message = 'error';
    });

}     

** Note the $.param function is a jQuery function, so you will have to include jQuery in your html file.

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

if (isset($_POST['programName'])) {   
   echo $_POST['programName'];
} else {
   echo 'Error Occured.'
}

?>
Joël Phaka
  • 61
  • 1
  • 1
  • 2