I am trying to make a simple contact form using PHP and AJAX. When called directly (via action="sendmail.php"
), the php form works fine, however when i try to do it with jquery AJAX i receive 405 Not Allowed.
Now the console returns:
n.ajaxTransport.k.cors.a.crossDomain.send @ code.jquery.com/jquery-2.1.4.min.js:4
n.extend.ajax @ code.jquery.com/jquery-2.1.4.min.js:4
n.each.n.(anonymous function) @ code.jquery.com/jquery-2.1.4.min.js:4
n.event.dispatch @ code.jquery.com/jquery-2.1.4.min.js:3
n.event.add.r.handle @ code.jquery.com/jquery-2.1.4.min.js:3
(anonymous function) @ contact.html:187
This seems to suggest a cross domain request, except it's not. The file is domain.com/contact.html, and the php document is domain.com/sendmail.php.
Here is the original request (aka the "anonymous function" on line 187)
$(document).ready(function() {
$('#submit').click(function() {
$.post("./sendmail.php", $("#contactform").serialize(), function(data){});
//line 187 ^^^
$('#success').html('<br> Message sent!<br><br>');
$('#success').hide(2000);
});
});
and sendmail.php (Edit: I did try turning on error reporting here and got no errors)
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$regarding = $_POST['regarding'];
$comments = $_POST['comments'];
$to = 'myemail@domain.com';
$subject = 'Contact Form';
$message = $name." would like to be contacted regarding " .$regarding. ". " .$name. " can be reached via email at ".$email." or via phone at " .$phone. ".
S/he also provided the following comments: ".$comments. ".";
$headers = 'From: phpmailer@domain.com';
$mail=mail($to, $subject, $message, $headers);
if($mail){
echo "1"; //success
}
else{
echo "0"; //failed
}
?>
any help would be appreciated. would also be interested to know why this doesn't seem to be working.