PHP 使用 GoDaddy 共享主机从 AngularJS 表单发送时 $_POST 为空
PHP $_POST is Empty when sent from AngularJS form with GoDaddy Shared Hosting
我正在努力在我的网站上设置联系表格,但似乎无法使 PHP 正常工作。我能够将电子邮件发送到我的电子邮件,但是我从 $_POST 方法中获得的一切都是空的。我对 PHP 很陌生,但对 AngularJS 很有经验。任何帮助将不胜感激。
PHP:
<?php
$to = 'name@domain.com';
$subject = $_POST["subject"];
$message = $_POST["message"];
$fromEmail = $_POST["email"];
$fromName = $_POST["name"];
$headers = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n".'Reply-To: '.$fromEmail."\r\n".'X-Mailer: PHP/'.phpversion();
mail($to, $subject, $message, $headers, "-femail.name@domain.com");
?>
HTML:
<form name="contactForm" ng-submit="submit()" novalidate>
<div class="row">
<div class="col-md-6" ng-class="{'has-error' : contactForm.firstName.$invalid && !contactForm.firstName.$pristine}">
<label for="firstName">First Name:</label>
<input type="text" placeholder="Joe" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="firstName" ng-model="firstName" required />
</div>
<div class="col-md-6" ng-class="{'has-error' : contactForm.lastName.$invalid && !contactForm.lastName.$pristine}">
<label for="lastName">Last Name:</label>
<input type="text" placeholder="Schmoe" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="lastName" ng-model="lastName" required />
</div>
</div>
<div class="row">
<div class="col-md-12" ng-class="{'has-error' : contactForm.email.$invalid && !contactForm.email.$pristine}">
<label for="email">Email Address:</label>
<input type="email" placeholder="jSchmoe@joeschmoe.gov" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="email" ng-model="email" required />
</div>
</div>
<div class="row">
<div class="col-md-12" ng-class="{'has-error' : contactForm.subject.$invalid && !contactForm.subject.$pristine}">
<label for="subject">Subject:</label>
<input type="subject" placeholder="Help me!" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="subject" ng-model="subject" required />
</div>
</div>
<div class="row">
<div class="col-md-12" ng-class="{'has-error' : contactForm.body.$invalid && !contactForm.body.$pristine}">
<label for="body">Body:</label>
<textarea style="width: 100%; height: 173px; resize: none;" type="subject" placeholder="I am stuck inside this website and want out! I have been here for years and years..." class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="body" ng-model="body" required />
</div>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<br />
<button type="submit" class="btn btn-success col-md-12" ng-disabled="contactForm.$invalid">
<h4>Send me an email!</h4>
</button>
</div>
</div>
</form>
JS:
window.dp.controller('ContactController', ['$scope', '$http', function($scope, $http) {
$scope.firstName = new String();
$scope.lastName = new String();
$scope.email = new String();
$scope.subject = new String();
$scope.body = new String();
$scope.submit = function() {
$scope.fullName = $scope.firstName + ' ' + $scope.lastName;
$scope.config = {
params: {
name: $scope.fullName,
email: $scope.email,
subject: $scope.subject,
message: $scope.body
}
};
$http.post("/php/contact.php", null, $scope.config)
.success(function(result){
// show success msg
}).error(function(result){
// show error msg
});
//
};
}]);
我仔细研究了这个问题,但大多数修复都是在 name
属性中添加到 HTML 输入标签。我确实看过 this 答案,但它似乎对我不起作用。
编辑:
刚刚找到 contact.php 的错误日志,内容如下:
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: subject in /home/[mydomainname]/public_html/dp/php/contact.php on line 3
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: message in /home/[mydomainname]/public_html/dp/php/contact.php on line 4
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: email in /home/[mydomainname]/public_html/dp/php/contact.php on line 5
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: name in /home/[mydomainname]/public_html/dp/php/contact.php on line 6
我将上面日志中的部分路径替换为 [mydomainname] 以隐藏它所在的域,并且还缩短了日志,因为我每次尝试提交时都会重复这 4 行一封电子邮件。
我也直接尝试用一组示例参数访问这个文件,但我得到的结果和以前一样,这让我觉得浏览器发出请求和文件之间发生了一些奇怪的事情contact.php 收到请求。
您 $http.post
的第二个参数应该是数据,但您有 null
.
阅读 http 手册 here。
所以我发现我做错了什么。显然,如果你想访问 HTTP POST 请求的参数,你必须使用 $_REQUEST
,而如果你想访问 HTTP POST 请求的数据,你必须使用 $_POST
.
我的新PHP如下:
<?php
$to = 'name@domain.com';
$subject = $_REQUEST["subject"];
$message = $_REQUEST["message"];
$fromEmail = $_REQUEST["email"];
$fromName = $_REQUEST["name"];
$headers = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n".'Reply-To: '.$fromEmail."\r\n".'X-Mailer: PHP/'.phpversion();
mail($to, $subject, $message, $headers, "-femail.name@domain.com");
?>
希望这可以帮助任何人解决这个问题!
我正在努力在我的网站上设置联系表格,但似乎无法使 PHP 正常工作。我能够将电子邮件发送到我的电子邮件,但是我从 $_POST 方法中获得的一切都是空的。我对 PHP 很陌生,但对 AngularJS 很有经验。任何帮助将不胜感激。
PHP:
<?php
$to = 'name@domain.com';
$subject = $_POST["subject"];
$message = $_POST["message"];
$fromEmail = $_POST["email"];
$fromName = $_POST["name"];
$headers = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n".'Reply-To: '.$fromEmail."\r\n".'X-Mailer: PHP/'.phpversion();
mail($to, $subject, $message, $headers, "-femail.name@domain.com");
?>
HTML:
<form name="contactForm" ng-submit="submit()" novalidate>
<div class="row">
<div class="col-md-6" ng-class="{'has-error' : contactForm.firstName.$invalid && !contactForm.firstName.$pristine}">
<label for="firstName">First Name:</label>
<input type="text" placeholder="Joe" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="firstName" ng-model="firstName" required />
</div>
<div class="col-md-6" ng-class="{'has-error' : contactForm.lastName.$invalid && !contactForm.lastName.$pristine}">
<label for="lastName">Last Name:</label>
<input type="text" placeholder="Schmoe" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="lastName" ng-model="lastName" required />
</div>
</div>
<div class="row">
<div class="col-md-12" ng-class="{'has-error' : contactForm.email.$invalid && !contactForm.email.$pristine}">
<label for="email">Email Address:</label>
<input type="email" placeholder="jSchmoe@joeschmoe.gov" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="email" ng-model="email" required />
</div>
</div>
<div class="row">
<div class="col-md-12" ng-class="{'has-error' : contactForm.subject.$invalid && !contactForm.subject.$pristine}">
<label for="subject">Subject:</label>
<input type="subject" placeholder="Help me!" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="subject" ng-model="subject" required />
</div>
</div>
<div class="row">
<div class="col-md-12" ng-class="{'has-error' : contactForm.body.$invalid && !contactForm.body.$pristine}">
<label for="body">Body:</label>
<textarea style="width: 100%; height: 173px; resize: none;" type="subject" placeholder="I am stuck inside this website and want out! I have been here for years and years..." class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="body" ng-model="body" required />
</div>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<br />
<button type="submit" class="btn btn-success col-md-12" ng-disabled="contactForm.$invalid">
<h4>Send me an email!</h4>
</button>
</div>
</div>
</form>
JS:
window.dp.controller('ContactController', ['$scope', '$http', function($scope, $http) {
$scope.firstName = new String();
$scope.lastName = new String();
$scope.email = new String();
$scope.subject = new String();
$scope.body = new String();
$scope.submit = function() {
$scope.fullName = $scope.firstName + ' ' + $scope.lastName;
$scope.config = {
params: {
name: $scope.fullName,
email: $scope.email,
subject: $scope.subject,
message: $scope.body
}
};
$http.post("/php/contact.php", null, $scope.config)
.success(function(result){
// show success msg
}).error(function(result){
// show error msg
});
//
};
}]);
我仔细研究了这个问题,但大多数修复都是在 name
属性中添加到 HTML 输入标签。我确实看过 this 答案,但它似乎对我不起作用。
编辑:
刚刚找到 contact.php 的错误日志,内容如下:
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: subject in /home/[mydomainname]/public_html/dp/php/contact.php on line 3
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: message in /home/[mydomainname]/public_html/dp/php/contact.php on line 4
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: email in /home/[mydomainname]/public_html/dp/php/contact.php on line 5
[20-Apr-2015 02:03:07 UTC] PHP Notice: Undefined index: name in /home/[mydomainname]/public_html/dp/php/contact.php on line 6
我将上面日志中的部分路径替换为 [mydomainname] 以隐藏它所在的域,并且还缩短了日志,因为我每次尝试提交时都会重复这 4 行一封电子邮件。
我也直接尝试用一组示例参数访问这个文件,但我得到的结果和以前一样,这让我觉得浏览器发出请求和文件之间发生了一些奇怪的事情contact.php 收到请求。
您 $http.post
的第二个参数应该是数据,但您有 null
.
阅读 http 手册 here。
所以我发现我做错了什么。显然,如果你想访问 HTTP POST 请求的参数,你必须使用 $_REQUEST
,而如果你想访问 HTTP POST 请求的数据,你必须使用 $_POST
.
我的新PHP如下:
<?php
$to = 'name@domain.com';
$subject = $_REQUEST["subject"];
$message = $_REQUEST["message"];
$fromEmail = $_REQUEST["email"];
$fromName = $_REQUEST["name"];
$headers = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n".'Reply-To: '.$fromEmail."\r\n".'X-Mailer: PHP/'.phpversion();
mail($to, $subject, $message, $headers, "-femail.name@domain.com");
?>
希望这可以帮助任何人解决这个问题!