0

My index.html:

<form name="myForm" ng-controller="Ctrl" ng-submit="save(user)">
  <label>Name:</label>
  <input type="text" ng-model="user.name"/><br /><br />
  <label>Email:</label>
  <input type="text" ng-model="user.email"/><br /><br />
  <input type="submit" value="Submit"/>
</form>

script.js

   function Ctrl($scope,$http)
   {

$scope.save = function(user)
{
   var data={
   name: user.name,
   email:user.email
        }
        console.log(data);
 $http.post("insert.php",data).success(function(data){
   console.log(data);
 });


}
 }      

insert.php

<? php

  $data = json_decode(file_get_contents('php://input'), true);
  if (json_last_error() === JSON_ERROR_NONE) {
  // use $data instead of $_POST
   print_r($data);

  ?>

This is my code to store form data in database.. but its not working...i am complete new to angularjs...i donno where i went wrong... pls help me..

2
  • insert.php - is this a valid url? Haven't used php in ages but I can't remember putting .php on the end of everything! Depends on your server config of course... You might put a .error() callback and see if that returns anything. Commented Aug 13, 2013 at 11:48
  • @IanHaggerty:its not giving anything Commented Aug 13, 2013 at 11:58

1 Answer 1

2

You have several errors:

JS (You need to $scope to link your variables)

$scope.save = function() {
   var data = {
      name: $scope.user.name,
      email: $scope.user.email
   }

   $http.post("insert.php", data).success(function(data, status, headers, config){

   });
}

HTML

<form name="myForm" ng-controller="Ctrl" ng-submit="save()">
  <label>Name:</label>
  <input type="text" ng-model="user.name"/><br /><br />
  <label>Email:</label>
  <input type="text" ng-model="user.email"/><br /><br />
  <input type="submit" value="Submit"/>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

still the same thing happening...its not going to insert.php...in success function if i say console.log(data) it displaying the complete php code...i donno y...

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.