0

The submitform() does not take the for datas to the function. Chrome console says ReferenceError: $ is not defined. Is there anything wrong in the code ? enter image description here

    var app = angular.module('app', []);
    app.controller('testimonialController', function($scope, $http) {
      $scope.formdata = {};
      $scope.submission = false;
      $scope.submitform = function($scope, $http) {
        $http({
          method: 'POST',
          url: 'sendmail.php',
          data: $.param($scope.formdata), // pass in data as strings
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          } // set the headers so angular passing info as form data (not request payload)
        }).
        success(function() {
          console.log("send successfuly");
        }).
        error(function() {
          console.log("It is failed");
        });
      };
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="contact-form" ng-controller="testimonialController" ng-app="app">
  <h1 id="contact">Contact</h1>
  <pre>Form data: {{formdata}}</pre>
  <p>Take a look around the site, Do you have something in mind you would like to let me know .You can contact me by filling the form below or email [email protected]</p>
  <form ng-submit="submitform()">
    <div class="form-wrap">
      <label for="name">Name</label>
      <input type="text" name="name" ng-model="formdata.name" placeholder="Name" required>
      <br>

    </div>
    <div class="form-wrap">
      <label for="email">Email</label>
      <input type="email" name="email" ng-model="formdata.email" placeholder="Email" required>
      <br>

    </div>
    <div class="form-wrap">
      <label for="comment">Message</label>
      <br>
      <textarea name="comment" id="comment" ng-model="formdata.comment" placeholder="Comment" cols="30" rows="10" required></textarea>
      <br>

    </div>
    <input type="submit" name="submit" value="Send">
  </form>
</div>
This is the console message when use enter image description here

$http({
    method  : 'POST',
    url     : 'sendmail.php',
    data    : $scope.formdata,  // pass in data as strings
    headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
}).
4
  • Why do you use data : $.param($scope.formdata) and not just $scope.formdata ? Commented Nov 8, 2014 at 2:20
  • @Jeyp I just followed this tutorial webdevmatters.wordpress.com/2014/07/28/… Commented Nov 8, 2014 at 2:26
  • @Jeyp Console error is TypeError: Cannot read property 'formdata' of undefined when remove $.param as data : $scope.formdata Commented Nov 8, 2014 at 2:31
  • The issue really is, that you dont include jQuery. However, is there a special reason why you use application/x-www-form-urlencoded as Content-Type? If not, just go with application/json and then it will be enough to just pass $scope.formdata as you did in your second approach. Commented Nov 8, 2014 at 12:14

1 Answer 1

1

data: $.param($scope.formdata)

This line uses jQuery's $.param method. You have to include it, if you want to use it

Sign up to request clarification or add additional context in comments.

4 Comments

I am not using jQuery in the function
@MuhammedAthimannil - Yes you are. I'm citing the article that you linked: Ok, I’ll be honest, there’s a quick cheat here. I’m using jQuery’s $.param to help me send data.
Any solution without using jQuery ?
@MuhammedAthimannil Yeah, you should read my comment on your original post.

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.