PHP Angular JS联系人表单将't张贴
PHP Angular JS contact form wouldn't post
当我试图发布到我的php表单时,我一直收到"请完整填写表单!"错误。
当我尝试提交联系表格时出现的错误这是我的角度
var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'pages/partial.home.html',
controller: 'homecontroller'
})
.when('/about', {
templateUrl: 'pages/partial.about.html',
controller: 'aboutcontroller'
})
.when('/services', {
templateUrl: 'pages/partial.services.html',
controller: 'servicecontroller'
})
.when('/contact', {
templateUrl: 'pages/partial.contact.html',
controller: 'contactcontroller'
})
.when('/portfolio', {
templateUrl: 'pages/partial.portfolio.html',
controller: 'portfoliocontroller'
});
/* .otherwise({
templateUrl:'pages/partial.404.html'
});*/
}
]);
app.controller('homecontroller', ['$scope', '$http',
function($scope, $http) {
$scope.title = 'About Us';
}
]);
app.controller('aboutcontroller', ['$scope', '$http',
function($scope, $http) {
$scope.title = 'About Us';
}
]);
app.controller('servicecontroller', ['$scope', '$http',
function($scope, $http) {
$scope.title = 'Our Services';
}
]);
app.controller('contactcontroller', ['$scope', '$http',
function($scope, $http) {
$scope.resultMessage;
$scope.result = 'hidden';
$scope.formData; //formData is an object holding the name, email, subject, and message
$scope.submitButtonDisabled = false;
$scope.submitted = false;
$scope.submit = function(contactform) {
$scope.submitted = true;
$scope.submitButtonDisabled = true;
if (contactform.$valid) {
$http({
method: 'POST',
url: 'bin/contact_form.php',
data: $.param($scope.formData), //param method from jQuery
headers: {
'Content-Type': 'application/x-www-form-urlenconded'
}
}).success(function(data) {
console.log(data);
if (data.success) { //success comes from the return json object
$scope.submitButtonDisabled = true;
$scope.resultMessage = data.message;
$scope.result = 'bg-success';
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = data.message;
$scope.result = 'bg-danger';
}
});
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = 'Oops, Failed to send!';
$scope.result = 'bg-danger';
}
}
}
]);
app.controller('portfoliocontroller', ['$scope', '$http',
function($scope, $http) {
$scope.title = 'Our portfolio';
}
]);
这里是我的php代码
<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
require 'class.phpmailer.php'; //include phpmailer
if (isset($_POST['inputName']) && isset($_POST['inputEmail']) && isset($_POST['inputSubject']) && isset($_POST['inputMessage'])) {
//check if any of the inputs are empty
if (empty($_POST['inputName']) || empty($_POST['inputEmail']) || empty($_POST['inputSubject']) || empty($_POST['inputMessage'])) {
$data = array('success' => false, 'message' => 'Please fill out the form completely.');
echo json_encode($data);
exit;
}
$usermail = $_POST['inputEmail']; //get sender mail address
$sender = $_POST['inputName']; // get sender Name
$from = "*@gmail.com"; //SMTP mail address
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP ?
$mail->IsHTML(false); // use HTML emails ?
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled
$mail->Host = "smtp.gmail.com"; //mail server Host
$mail->Port = 465; // set the SMTP port
$mail->Username = "*@gmail.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->SetFrom($from, 'Contact Form'); //
$mail->AddReplyTo($usermail, $sender); // set reply to sender
$mail->AddAddress('*@gmail.com','***'); //recipient email address
$mail->Subject = $_POST['inputSubject'];
$mail->Body = "Name: " . $_POST['inputName'] . "'r'n'r'n Email: ".$_POST['inputEmail']."'r'n'r'nMessage: " . stripslashes($_POST['inputMessage']);
$mail->WordWrap = 50;
if (isset($_POST['ref'])) {
$mail->Body .= "'r'n'r'nRef: " . $_POST['ref'];
}
if(!$mail->Send()) {
$data = array('success' => false, 'message' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo);
echo json_encode($data);
exit;
} else {
$data = array('success' => true, 'message' => 'Thanks! We have received your message.');
echo json_encode($data);
}
} else {
$data = array('success' => false, 'message' => 'Please fill out the form completely.');
echo json_encode($data);
}
?>
我强烈建议不要将JQuery与Angularjs一起使用。
发生这种情况是因为您没有正确发送表单参数,请尝试将数据更改为以下内容:
$http({
method: 'POST',
url: 'bin/contact_form.php',
data: $scope.formData,
headers: {
'Content-Type': 'application/x-www-form-urlenconded'
}}).success ....
我希望它能有所帮助。
我用这种方式进行Ajax调用,将数据发布为json:
控制器内:
$http.post('bin/contact_form.php', $scope.formData).success(function(data) {
console.log(data);
});
在您的PHP文件中:
$form = json_decode(file_get_contents("php://input"));
if ( $form->inputName ... )
edit:$http.post()
只是普通$http
方法的快捷方式,但默认情况下Content-Type
设置为application/json
。在php中,您将通过上面的代码获得json对象。对我来说,打字比$_POST[...]
好。
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 使用angular重定向到html页面
- angular.js没有'无法在PhoneGap中处理视图标记
- Javascript(Angular)从一个对象数组到第二个数组查找值
- angular 1.5应用程序中的导航栏
- angular的下拉菜单
- Angular只从数组中获取所需的数据
- 如何将不可变的js导入angular 2(alpha)
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将JSON对象传递给angular指令
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Angular:更新一次性绑定的数据
- 同步调用,直到用户通过angular验证为访问者
- 将Angular js与taglib结合使用
- PHP Angular JS联系人表单将't张贴
- 跨域问题,而张贴在第三方网站在angular js