[英]I am trying to send an email via angular. Am I approaching this in the correct manner?
I'm just trying to learn something new. 我只是想学习新东西。 I want to be able to send an email using angular and php. 我希望能够使用angular和php发送电子邮件。 I am turning my form data into an object. 我正在将表单数据变成一个对象。 Then I want to send this object as a variable and send the data to a php file. 然后,我想将此对象作为变量发送,并将数据发送到php文件。 I am able to create the variable but I don't believe the data is being set in the php file. 我能够创建变量,但是我不相信数据是在php文件中设置的。 What am I missing here? 我在这里想念什么?
angular.module('app', []); var vm = this; var url = '/contact-form-handler.php'; vm.codeStatus = ""; vm.form = {}; vm.submit = function($event) { $event.preventDefault(); console.log(this.form); $http({ method: "POST", url: url, data: vm.form, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }). success(function(result) { console.log(result); console.log('hit'); }). error(function(result) { console.log('error'); }); return false; };
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <?php header('Access-Control-Allow-Origin: *'); if (isset($_POST["data"])) { $from = 'Portfolio'; $to = 'rafael.heard@gmail.com'; $subject = 'Portfolio Inquiry'; $body = $_POST['data']; mail($to, $subject, $body, $from); $result = 'mail sent'; }else{ $result = 'not set'; } ?> <div ng-app="app" ng-controller="MainController as main"> <form role="form" name="contactForm" ng-submit="main.submit($event)" required novalidate> <div class="form-groups"> <div class="group"> <input type="text" name="username" ng-model="main.form.name" placeholder="Name (required)" required /> </div> <div class="group"> <input type="email" name="userEmail" ng-model="main.form.email" placeholder="Email (required)" required /> </div> <div class="group"> <textarea type="text" name="message" ng-model="main.form.message" ng-minlength="10" rows="10" required></textarea> </div> <button>Send</button> </div> </form>
I figured this one out. 我想通了。 I change my approach in my php file, and the angular http. 我在我的php文件和尖角http中更改了方法。 I serialized the object. 我序列化了对象。
var url = 'contact-form-handler.php'; vm.codeStatus = ""; vm.form = {}; vm.submit = function($event) { // $event.preventDefault(); console.log($.param(vm.form)); $http({ method: "POST", url: url, data: $.param(vm.form), headers: {'Content-Type': 'application/x-www-form-urlencoded'} }). success(function(result) { console.log(result); }). error(function(data) { console.log('not sent'); }); return false; }; }]);
<?php header('Access-Control-Allow-Origin: *'); if (isset($_POST["subject"])) { $name = $_POST['name']; $subject = $_POST['subject']; $from = $_POST['email']; $message = $_POST['message']; $to = 'rafael.heard@gmail.com'; mail($to, $subject, $message, $from); }else{ echo 'not set'; } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.