0

Requirement - post data from angularJS controler to php file save data to database and give resonse to angularJS controller

Issue - I am posting some data from angularJS controller to php file and want response from there but having incorrect response like this

<?php $data = json_decode(file_get_contents("php://input")); $fname =$data->firstName; $arr = array('msg' => "User Created Successfully!!!", 'error' => ''); $jsn = json_encode($arr); print_r($jsn); ?> 

i.e the whole content of php file.

My controller function look like this -

app.controller('manageController',function($scope,$http){
    $scope.addUser=function(user,event){
        var userJson=angular.toJson(user);  
           alert(userJson);     
        var responsePromise =$http({
                method: "post",
                url: "addUSer.php",
                data:userJson,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });

        responsePromise.success(function(data, status, headers, config) {
            $scope.fromServer = data;           
        });
        responsePromise.error(function(data, status, headers, config) {
            alert("Failed!");
        });
    }   
});

And php file look like this -

<?php
    $data = json_decode(file_get_contents("php://input"));
    $fname =$data->firstName;
     $arr = array('msg' => "User Created Successfully!!!", 'error' => '');
        $jsn = json_encode($arr);
        print_r($jsn);
?>

Browser giving error "no element found" in php file when i open it, its showing part before "$data->" in red color and remaining part in black color.

Can anyone give some suggestions on it.

Amit Sirohiya
  • 333
  • 1
  • 13

0 Answers0