AngularJS联系表

时间:2014-07-18 14:55:47

标签: javascript php forms angularjs

我对AngularJS很新,并试图建立一个联系表单。我做了一些研究,以角度验证,这工作正常。但是,将输入发送到我的电子邮件帐户仍然是一个谜。

HTML代码段

   <form name="contactForm" ng-submit="submitForm(contactForm.$valid)" novalidate> <!-- novalidate prevents HTML5 validation since we will be validating ourselves -->

        <!-- NAME -->
        <div class="form-group" ng-class="{ 'has-error' : contactForm.name.$invalid && !contactForm.name.$pristine }">
            <input type="text" name="name" class="form-control" ng-model="formData.name" placeholder="Volledige naam" ng-minlength="3" required>
            <p ng-show="contactForm.name.$error.minlength" class="help-block">Je naam lijkt iets te kort, vul ook je achternaam in!</p>
        </div>

        <!-- EMAIL -->
        <div class="form-group" ng-class="{ 'has-error' : contactForm.email.$invalid && !contactForm.email.$pristine }">
            <input type="email" name="email" class="form-control" ng-model="formData.email" placeholder="Email adres" required>
            <p ng-show="contactForm.email.$invalid && !contactForm.email.$pristine" class="help-block">Voer een geldig email adres in</p>
        </div>

        <!-- TEL -->
        <div class="form-group" ng-class="{ 'has-error' : contactForm.tel.$invalid && !contactForm.tel.$pristine }">
            <input type="tel" name="tel" class="form-control" ng-model="formData.tel" placeholder="Telefoonnummer" min-length="10" required>
            <p ng-show="contactForm.tel.$error.minlength" class="help-block">Je telefoonnummer lijkt iets te kort, misschien mis je iets als "+31" of "043"</p>
        </div>

        <!-- TEXT -->
        <div class="form-group" ng-class="{ 'has-error' : contactForm.text.$invalid && !contactForm.text.$pristine }">
            <textarea name="text" class="form-control" ng-model="formData.text" placeholder="Formuleer uw situatie kort. Geef ook aan op welke dagen u beschikbaar bent of gebeld wenst te worden." ng-minlength="10" required></textarea>
            <p ng-show="contactForm.text.$error.minlength" class="help-block">Vertel ons iets meer...</p>
        </div>

        <!-- SUBMIT BUTTON -->
        <input type="submit" ng-submit="processForm()" ng-disabled="contactForm.$invalid" value="Maak afspraak">

<!--         <pre>
        {{formData}}
        </pre> -->

    </form>

控制器

.controller('MainCtrl', function($scope, $http) {
  this.features = keys;
    $scope.submitForm = function(isValid) {
      if (isValid) {
        $scope.formData = {};

        $scope.processForm = function() {
          $http({
            method  : 'POST',
            url     : 'process.php',
            data    : $.param($scope.formData),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
          });
        };
      }
    };
})

process.php文件仍然是空的,因为我不太清楚放在这里的内容。

2 个答案:

答案 0 :(得分:2)

关于表单提交设置,您不能将ng-submit放在任何输入[type =“submit”]元素上,而是放置ng-click。此外,根据the angular docs,当您在表单上进行ng-submit,并且对任何输入[type =“submit”]元素进行ng-click时,将首先调用ng-click处理程序,然后将调用ng-submit处理程序(您的控制器设置为相反的顺序)。

但是在你的情况下,两个提交处理程序是不必要的,因为你可以使用angular指令进行所有验证,只需使用ng-click。 Theres an example plunker here

<强> HTML:

    <div ng-controller="MainCtrl">
      <form name="contactForm" novalidate>
        <!-- NAME -->
        <div ng-class="{'form-error':contactForm.name.$dirty && contactForm.name.$invalid, 'form-group':true}">
          <input type="text" name="name" ng-model="formData.name" placeholder="Volledige naam" ng-minlength="3" required="" />
          <div ng-messages="contactForm.name.$error" ng-show="contactForm.name.$dirty" >
            <div ng-message="minlength">Name too short</div>
            <div ng-message="required">Required Name</div>
          </div>
        </div>
        <!-- EMAIL -->
        <div ng-class="{'form-error':contactForm.email.$dirty && contactForm.email.$invalid, 'form-group':true}">
          <input type="email" name="email" ng-model="formData.email" placeholder="Email adres" required />
          <div ng-messages="contactForm.email.$error" ng-show="contactForm.email.$dirty">
            <div ng-message="email">Invalid Email</div>
            <div ng-message="required">Required Email</div>
          </div>
        </div>
        <!-- TEL -->
        <div ng-class="{'form-error':contactForm.tel.$dirty && contactForm.tel.$invalid, 'form-group':true}">
          <input type="text" name="tel" ng-pattern=/\d{3}-\d{3}-\d{4}/ ng-model="formData.tel" placeholder="Telefoonnummer" required />
          <div ng-messages="contactForm.tel.$error" ng-show="contactForm.tel.$dirty">
            <div ng-message="pattern">Valid form: XXX-XXX-XXXX</div>
            <div ng-message="required">Required Phone</div>
          </div>
        </div>
        <!-- SUBMIT BUTTON -->
        <input type="submit" ng-click="processForm()" ng-disabled="contactForm.$invalid" value="Maak afspraak" />
      </form>
    </div>

<强> JavaScript的:

.controller('MainCtrl', function($scope, $http) {
  $scope.formData = {};
  $scope.processForm = function() {
    alert('valid form!')
    $http({
      method  : 'POST',
      url     : 'process.php',
      data    : $scope.formData,
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
    });
  };
});

关于提交到您的电子邮件,$ http服务将使用http协议发布您的数据,因此您无法直接将其发送给您的电子邮件,因为电子邮件不使用HTTP协议进行通信。您可以将发布数据发送到您的服务器,并让服务器发送电子邮件(例如,如果您使用Node,则可以使用nodemailer package发送电子邮件)。

答案 1 :(得分:0)

只需复制angularJs项目中的代码即可正常工作

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="container">
  <div class="row" style="margin-bottom:15px;">
    <div class="col-md-12">
      <div class="col-md-offset-1 col-md-6" id="box">
        <h2 class="colr">Contact Us!</h2>
        <hr>
        <form class="form-horizontal" name="contactForm" ng-submit="submitContactForm(ContactDetails)" novalidate>
          <fieldset>
            <!-- Form Name -->
            <!-- Text input-->
            <div class="form-group">
              <div class="col-md-12" ng-class="{ 'has-error' : contactForm.first_name.$invalid && !contactForm.first_name.$pristine }">
                <div class="input-group">
                  <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                  <input name="first_name" placeholder="Name" ng-model="ContactDetails.FirstName" class="form-control" type="text" required>
                </div>
                <span ng-show="contactForm.first_name.$invalid && !contactForm.first_name.$pristine" class="help-block">Username is required.</span>
              </div>
            </div>
            <!-- Text input-->
            <div class="form-group">
              <div class="col-md-12" ng-class="{ 'has-error' : contactForm.email.$invalid && !contactForm.email.$pristine }">
                <div class="input-group">
                  <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
                  <input name="email" placeholder="E-Mail Address" ng-model="ContactDetails.Email" class="form-control" type="text" ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/">
                </div>
                <span ng-show="contactForm.email.$invalid && !contactForm.email.$pristine" class="help-block"> Enter a valid email.</span>
              </div>
            </div>
            <!-- Text input-->
            <div class="form-group" ng-class="{ 'has-error' : contactForm.phone.$invalid && !contactForm.phone.$pristine }">
              <div class="col-md-12">
                <div class="input-group">
                  <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
                  <input type="text" class="form-control" ng-model="ContactDetails.PhoneNumber" name="phone" ng-maxlength="10" ng-minlength="10" placeholder="9845640899" required>
                </div>
                <span ng-show="contactForm.phone.$invalid && !contactForm.phone.$pristine">Phone Number is required.</span>
              </div>
            </div>
            <div class="form-group" ng-class="{ 'has-error' : contactForm.subject.$invalid && !contactForm.subject.$pristine }">
              <div class="col-md-12">
                <div class="input-group">
                  <span class="input-group-addon"><i class="glyphicon glyphicon-bookmark"></i></span>
                  <input name="subject" ng-model="ContactDetails.Subject" placeholder="Enter Subject" class="form-control" type="text" required>
                </div>
                <span ng-show="contactForm.subject.$invalid && !contactForm.subject.$pristine" class="help-block">Subject is required.</span>
              </div>
            </div>
            <!-- Text input-->
            <div class="form-group" ng-class="{ 'has-error' : contactForm.comment.$invalid && !contactForm.comment.$pristine }">
              <div class="col-md-12 inputGroupContainer">
                <div class="input-group">
                  <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
                  <textarea class="form-control" ng-model="ContactDetails.Comment" rows="6" name="comment" placeholder="Message" ng-maxlength="255" ng-minlength="10" required></textarea>
                </div>
                <span ng-show="contactForm.comment.$invalid && !contactForm.comment.$pristine" class="help-block"> Enter a Message.</span>
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <button type="submit" class="btn btn-warning pull-right" ng-disabled="contactForm.$invalid">Send<span class="glyphicon glyphicon-send"></span>
                </button>
              </div>
            </div>
          </fieldset>
        </form>
      </div>

    </div>
  </div>
</div>