So I'm trying to setup a simple feedback form in Angular that will send an email using php on the backend. The problem is that I keep getting the following error:
POST https://localhost/mailer/contact-form.php 405 (Method Not Allowed)
405 Method Not Allowed
The method POST is not allowed for this resource.
You cannot POST a file
I think it has something to do with CORS, but don't know if it's more related to HTTPS since we are running on https://localhost for development.
Here's what I think is the important portion of the controller:
testControllers.controller('FeedbackCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.feedback_errors = "";
$scope.hasError = false;
$scope.resultMessage;
$scope.formData = {};
$scope.submitButtonDisabled = false;
$scope.submitted = false; //used so that form errors are shown only after the form has been submitted
var param = function(data) {
var returnString = '';
for (var d in data){
if (data.hasOwnProperty(d))
returnString += d + '=' + data[d] + '&';
}
// Remove last ampersand and return
return returnString.slice( 0, returnString.length - 1 );
};
$scope.submit = function(feedbackForm) {
$scope.submitted = true;
$scope.submitButtonDisabled = true;
if (feedbackForm.$valid) {
$http({
method : 'POST',
url : 'mailer/contact-form.php',
data : param($scope.formData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data){
console.log(data);
if (data.success) {
$scope.submitButtonDisabled = true;
$scope.resultMessage = data.message;
} else {
$scope.submitButtonDisabled = false;
$scope.resultMessage = data.message;
}
});
} else {
$scope.submitButtonDisabled = false;
}
}
}]);
and here is the php:
<?php
header('Access-Control-Allow-Origin: https://localhost');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
$errors = array();
$data = array();
if (empty($_POST['comments']))
$errors['comments'] = 'Feedback is required.';
if ( ! empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
$data['messageError'] = 'Please check the fields in red';
} else {
$data['success'] = true;
$data['messageSuccess'] = 'Thanks for providing valuable feedback to the VIVA team.';
$email_to = "jknowles@intellisis.com";
$email_subject = "Feedback from VIVA webapp";
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$email_from = $_POST['email'];
$message = $_POST['comments']; // required
$email_message = "Form details below.nn";
$email_message .= "Name: ".$name."n";
$email_message .= "Email: ".$email_from."n";
$email_message .= "Message: ".$message."n";
$headers = 'From: '.$email_from."rn".
'Reply-To: '.$email_from."rn" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}
// return all our data to an AJAX call
echo json_encode($data);
?>
I've tried so many different combinations for the header of the php file and one thing I'm wondering is if I'm missing something on the front end? Any help will be immensely appreciated as I've been banging my head against the wall for hours.
Best Regards, Julie
EDIT:
So I've created a simple form using Angular and I run it on one of my own servers and it works fine.
Here are response headers from both the one that works and the one that doesn't work:
DOESN'T WORK
General:
Remote Address:xx.xx.xx.xx:yyy
Request URL:https://xx.xx.xx.xx/form_test/processForm.php
Request Method:POST
Status Code:405 Not Allowed
Response Headers
Connection:keep-alive
Content-Length:574
Content-Type:text/html
Date:Mon, 06 Apr 2015 18:44:20 GMT
Server:nginx/1.6.2
WORKS
General:
Remote Address:xx.xx.xx.xx:yyy
Request URL:http://juliemarie.me/form_test/processForm.php
Request Method:POST
Status Code:200 OK
Response Headers
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Type:text/html
Date:Mon, 06 Apr 2015 18:37:19 GMT
Keep-Alive:timeout=15, max=100
Server:Apache
Transfer-Encoding:chunked
It almost seems like the php file isn't even accessed. Is that possible?