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.

9
  • did you try to hit addUSer.php in your browser? what do you get? Commented Apr 2, 2015 at 11:40
  • Have you given this a look? stackoverflow.com/questions/29122238/… Commented Apr 2, 2015 at 11:40
  • @yashhy after hitting it, its showing firstName; $arr = array('msg' => "User Created Successfully!!!", 'error' => ''); $jsn = json_encode($arr); print_r($jsn); ?> Commented Apr 2, 2015 at 11:42
  • 1
    On your addUSer.php try "print_r($_POST); " to see what you're getting from angular. Commented Apr 2, 2015 at 11:50
  • 1
    By the way, you're sure your webserver is running php, right? Commented Apr 2, 2015 at 11:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.