【问题标题】:AngularJS Contact FormAngularJS 联系表
【发布时间】:2014-07-18 14:55:46
【问题描述】:

我对 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 文件仍然是空的,因为我不太确定该放什么。

【问题讨论】:

  • 无需使用 $.param 。如果您正在学习 Angular,请直接从页面中取出 jQuery 库来开始
  • @charlietfl 好的,去掉“$”?
  • no.. 只需传递对象,Angular 将负责适当的序列化

标签: javascript php forms angularjs


【解决方案1】:

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

但是在您的情况下,两个提交处理程序是不必要的,因为您可以使用角度指令进行所有验证,并且只需使用 ng-click。有一个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 包发送电子邮件)。

【讨论】:

  • 谢谢,我试试nodemailer
【解决方案2】:

只需将以下代码复制到您的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多