0

I'm Working on AngularJs about a week and I'm trying to insert a value into mysql database through a PHP.

my form look like this

    <form ng-model="far_form" name="far_reg" ng-submit="register(far_reg.$valid)" novalidate="true" 
        ng-controller= "farform_contr">
        <input type="text" id="First Name" placeholder="Name" ng-model="far.first_name" >
        <input type="text" id="Email" placeholder="Email" ng-model="far.email">
        <input type="text" id="Mobile" placeholder="Mobile" ng-model="far.mobile">
    </form>

And Controler is

.controller('farform_contr',['$scope', '$http', function($scope, $http) {
        $scope.register = function(isValid) {
            if (isValid) {
                $http({
                        method  : 'POST',
                        url     : 'submit.php',
                        dataType: 'json',
                        data    : $scope.far, //forms user object
                        headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
                }).
                        success(function(data, status) {
                            console.log(far);
                            $scope.status = status;
                            $scope.data = data;
                        })
            }else{

                  alert('Form is not valid');
            }
        }
    }]);

I have couple of other controller in the same file Which are working fine.

submit.php is

<?php
    $post_date = file_get_contents("php://input");
    $data = json_decode($post_date);
    $name=$data['first_name'];
    $email=$data['email'];
    $mobile=$data['mobile'];
    echo "<script>alert('$name')</script>";
    echo "<script>alert('$email')</script>";
    echo "<script>alert('$mobile')</script>";
?>

All the echo's are returning null I don't know where i'm going wrong.

1
  • When you use $http service, the way of retrieving response is argument.data in success function. Have you got any response in success? Commented May 22, 2016 at 8:30

1 Answer 1

1

Probably need to serialize the json before decoding it and specify the content type to "application/json"

$http({
method  : 'POST',
url     : 'submit.php',
dataType: 'json',
data    : JSON.stringify($scope.far), //forms user object
headers : {'Content-Type': 'application/json'} 
}).

UPDATE:

Initiliaze the $scope.far in the controller before the $scope.register function as:

$scope.far = {first_name : '', email:'', mobile:''};
Sign up to request clarification or add additional context in comments.

3 Comments

I used alert for $scope.far after if in controller.js its giving undefined instead of Object.@Dev-One
Why are you stringifying or setting json Content Type? These are done by default by $http. Just remove headers completely
Would it be an issue if the user still stringify the payload before posting, since its already been handled by $http?

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.