1

I want to insert user data into database but I can't send from data through AngularJS and I am new to AngularJS. I check by print_r($_POST) in insert.php but it displays as empty array.

How can I send form data using PHP?

HTML

<form name="form" ng-submit="signup()">
    <div class="form-group">
        <label for="Name">Name</label>
        <input type="text" class="form-control" placeholder="Enter your  name" name="name" ng-pattern="/^[a-zA-Z]*$/" required ng-model="user.name">
        <span ng-show="form.name.$dirty && form.name.$error.required">Enter  your name</span>
        <span ng-show="form.name.$dirty && form.name.$error.pattern">Name in  characters only</span>
    </div>

    <div class="form-group">
        <label for="Mobile">Mobile Number</label>
        <input type="text" class="form-control" placeholder="Enter your mobile number" name="mobile" required ng-minlength="10" ng-maxlength="10" ng-model="user.mobile">
        <span ng-show="form.mobile.$dirty &&  form.mobile.$error.required">Enter your mobile number</span>
        <span ng-show="form.mobile.$dirty && (form.mobile.$error.minlength || form.mobile.$error.maxlength)">Mobile number should be 10 digits</span>
    </div>

    <div class="form-group">
        <label for="Name">Email</label>
        <input type="email" class="form-control" placeholder="Enter your email" name="email" required ng-model="user.email">
        <span ng-show="form.email.$dirty && form.email.$error.required">Enter your email</span>
        <span ng-show="form.email.$dirty && form.email.$error.email">Enter a valid email</span>
    </div>

    <div class="form-group">
        <label for="pwd">Password</label>
        <input type="password" class="form-control" placeholder="Enter password" name="password" required ng-minlength="8" ng-model="user.password">
        <span ng-show="form.password.$dirty && form.password.$error.required">Enter your password</span>
        <span ng-show="form.password.$dirty && form.password.$error.minlength">Password should be 8 characters</span>
    </div>

    <button class="btn btn-primary btn-block" name="login" id="login_btn" ng-disabled="form.$invalid">Sign up</button>
</form>

<br><br>

<p class="text-center" id="new"><span>Already have an account</span></p>
<a href="login.php" class="btn btn-default btn-block">Signin</a>
</div>
</div>
</body>
</html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1  /jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

JS

var app = angular.module('MyApp', []);

app.controller('MyCtrl', function($scope, $http) {
    $scope.signup = function(){    
        var data = $scope.user; 

        $http.post("insert.php", data).then(function(resp){        
            console.log(resp);
        });
    }
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bhaskararao Gummidi
  • 1,605
  • 3
  • 12
  • 14

2 Answers2

1

I tried and found solution to this question raised by me...thank you everyone.

    var app = angular.module('myApp', []);
        app.controller('MyCtrl', function($scope, $http) {
          $scope.signup = function(user) {
            var data = user;
            console.log(user)
            $http({
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                method:"POST",
                url:"insert.php",
                data: $.param({'data':user}),
                dataType:"json",

           });        
        }
    });
Bhaskararao Gummidi
  • 1,605
  • 3
  • 12
  • 14
0

First of all, put your script tag's inside html tag.

Secondly your $scope.signup function is missing a semicolon.

Try declaring a user in the start of your controller.

$scope.user = {};

And you need to give your button element a type of a submit if you are using ng-submit

<button type="submit"

It is a good practice to declare all your objects in the controller. Declaring in the template might get you where you do not want to go.