0

I just started learning AngularJS and will be using PHP for the backend.

I got the basics of Angular working, but now I'm at the point of form submission. Now I'm thinking that I could just do an $.ajax({...}); call to a PHP file and include that function at the bottom of my view.

However, I see there are other ways using controllers, which I'm researching; I just want to know if that would be considered bad practice or if there is a simple way to transition from one method to the other.

Thanks.

EDIT:

Here's what I did now and it's working great in my opinion. Doesn't hurt the integrity of Angular, the form submits, and all is good. Is there a downside to this:

function submitNewPatient(){

        $.ajax({
              url: 'ajax/patients/new_patient.php',
              type: 'post',
              data: $("#new_patient_form").serialize(),
              success: function() 
              {
                  $("#new_patient_form")[0].reset();                                          
              }, error: function()
              {
                  alert('something went wrong');
              }
        });
}

1 Answer 1

0

You'll want to look into the $http method.

Here's an example:

$http.post('api/user', {params: {id: '5'}
  }).success(function(data, status, headers, config) {
    // Do something successful.
  }).error(function(data, status, headers, config) {
    // Handle the error
});

Along with .get the $http service has .head, .post, .delete, .put, .jsonp. In your case $http.post, the params and URL in the example above evaluate to api/user?id=5

Also, forms in Angular come with an ng-submit directive which is very useful for calling a function on submission. The ng-submit prevents the browser from its default POST action. This way you can use it to call a function on the controller attached to the view.

<form ng-submit="sendForm()" ng-controller="FormSubmission">
   <button>send form!</button>
</form>
2
  • Ok thanks for that - I'll look into it's usage - Please see my edit and your feedback is appreciated. Commented Dec 28, 2014 at 4:59
  • Angular works well with Jquery but it's really meant for DOM manipulation in the directive. You're stepping outside the "Angular world" here, which is the downside. I've done a good amount of refactoring from jquery to angular, more concise, modular and more maintainable. Commented Dec 28, 2014 at 5:17

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.