0

My AngularJs page

  var postContactFn = function($http,data){
        return $http.post("myfirstfile.php", data);
    }

mainModule.factory('postContact',['$http',postContactFn]);

//In my controller

    postContactFn(data).success(function(data, status) {
        if(data){
                  console.log('success');
          }
    }

Php page MyFirstFile.php

 <?php
$name = $_POST['inputUsername']; 
if(!empty($name)){
  --send mail
 echo 'success'
}

Here the mail is been sent to my id but I'm not getting redirected back the controller. Instead it is showing success on my page.

Any suggestions would be appreciated.. Thank you

1 Answer 1

1

You pass the data as dependency, not to a function.

This is how it should be:

mainModule.controller('MyController', function($scope, postContact) {
  postContact.postData(data).then(function(data)) {
    console.log(data);
  });
});

And your factory:

mainModule.factory('postContact', function($http) {
  return {
    postData : function(data) {
      return $http.post("myfirstfile.php", data);
    }
  }
});
Sign up to request clarification or add additional context in comments.

7 Comments

okay but still it doesn't come back the controller it goes to the php file and shows success string on the browser. But actually it should get call backed in that controller.
I don't understand. what should call back in the controller?
I expect this should happen Controller -----> Factory----->PHP----on success--->Controller console.log('success') but on success php isn't passing to controller
Can you provide a plunkr?
plnkr.co/edit/RrneW4DM73Ay4xpsdJKj?p=preview This is the link but there are some other errors
|

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.