gpt4 book ai didi

javascript - 我正在尝试通过 Angular 发送电子邮件。我是否以正确的方式处理这个问题?

转载 作者:行者123 更新时间:2023-11-28 06:10:50 24 4
gpt4 key购买 nike

我只是想学习一些新东西。我希望能够使用 Angular 和 php 发送电子邮件。我正在将表单数据转换为对象。然后我想将此对象作为变量发送并将数据发送到 php 文件。我能够创建变量,但我不相信 php 文件中设置了数据。我在这里缺少什么?

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>

最佳答案

我想出了这个。我改变了我的 php 文件和 Angular http.我序列化了该对象。

    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';
}
?>

关于javascript - 我正在尝试通过 Angular 发送电子邮件。我是否以正确的方式处理这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com