1

I am trying to save form data in Angular by ajax to store.php file. But, seems data is not properly fetched by file. Can someone point the mistakes?

<form id="form1" post="content/store.php">

    <div class="form-group">
        <label for="examplename">Name</label>
        <input type="text" class="form-control" id="examplename" placeholder="Enter your name">
    </div>

    <div class="form-group">
        <label for="exampleemail">Email</label>
        <input type="email" class="form-control" id="exampleemail" placeholder="Enter your email">
    </div>

    <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          

</form>

ANGULAR SCRIPT:

var app = angular.module('myApp',[]);
app.controller ('myCtrl',function($scope, $http){
    $scope.submitting = function(){

        var request = $http({
            method: "post",
            url: "localhost/content/store.php",
            data: {
                email: $scope.email,
                pass: $scope.password
            }
        });

        request.success(function (data) {
            alert("Successfully data entered! ");
        });

    }

}); 

STORE.PHP

<?php
  $postdata = file_get_contents("php://input");
  $request = json_decode($postdata);
  @$email = $request->email;
  @$pass = $request->pass;
  echo $email; 
?>
1
  • try $_POST instead of file_get_contents("php://input"); Commented Jul 15, 2015 at 11:18

2 Answers 2

2

if in your controller you want to use

email: $scope.email

and

name: $scope.name

or password, you will need to have ng-model binding in your view like so:

<form id="form1" post="content/store.php">

    <div class="form-group">
        <label for="examplename">Name</label>
        <input type="text" class="form-control" id="examplename" placeholder="Enter your name" ng-model="name">
    </div>

    <div class="form-group">
        <label for="exampleemail">Email</label>
        <input type="email" class="form-control" id="exampleemail" placeholder="Enter your email" ng-model="email">
    </div>

    <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          

</form>
Sign up to request clarification or add additional context in comments.

Comments

1

You should have declared the your inputs ng-model

i.e.,

<input type="email" ng-model="email" class="form-control" id="examplename" placeholder="Enter your name">
<input type="password" ng-model="pass" class="form-control" id="exampleemail" placeholder="Enter your email">

So, that you can access it by

email: $scope.email,
pass: $scope.password

Note :

If you wish to have the input field as password, then change the type="password"

Or

If you wish to have the inputs as it is then in your controller just change

$scope.email and $scope.name But it is always better to declare those fields in the inputs i.e., ng-model="email" and ng-model="pass"

Comments

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.