Note: I have read other questions and they did not help.
So I first tested on my localhost with ajax and PHPMailer and it worked perfectly as I received the email and with ajax it said email sent.
When I uploaded to a live site, it stopped working. And I'm sure the connection is solid because when I take away AJAX and just put it as a PHP page it works. But, when its an external file with AJAX it doesn't (on a live website)
Here is the AJAX Code:
<script>
var ajax = {
isSubmiting: false,
send: function() {
if(ajax.isSubmiting == false) {
ajax.isSubmiting = true;
var userName = $("input[name=name]").val();
var userEmail = $("input[name=email]").val();
var userComments = $("textarea").val();
var currentBusiness = $("input[name=business").val();
var currentWebsite = $("input[name=website]").val();
ajax.SetText("Sending...");
$.post("sendmail.php", {
name: userName, email: userEmail, comments: userComments, business: currentBusiness, website: currentWebsite
}, function(data) {
if(data == "true") {
ajax.SetText("Sent!");
$.get("sent.html", function(sentData){
$("#content").html(sentData);
});
} else {
ajax.SetText("Send mail");
$.get("unsent.html", function(sentData){
$("#content").html(sentData);
});
console.log();
}
ajax.isSubmiting = false;
});
}
else alert("You can only send 1 email at a time");
},
SetText: function(text) {
$("input[type=button]").val(text);
}
}
</script>
And here is the PHPMailer script (sendmail.php):
<?php
$result = "";
$error = "";
if(count($_POST) > 0) {
$message=
'Full Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['comments'].'<br />
Current Website: '.$_POST['website'].'<br />
Business Name: '.$_POST['business'].'<br />
';
include "phpmailer/class.smtp.php";
include("includes/class.phpmailer.php");
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->Host = localhost;
//Set who the message is to be sent from
$mail->setFrom('info@coherenthub.com', 'Coherent');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('coherenthub@gmail.com', 'Coherent');
//Set the subject line
$mail->Subject = 'Contact Form';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->MsgHTML($message);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
die("There was an error sending the email.");
} else {
die("true");
}
}
?>
Does anyone have a clue as to why this wouldn't be said. Just to reiterate, on a single PHP page without ajax and with the script in the it works fine on a live and local server. BUT, when I put it in an external file and link with AJAX, it only works in local server but not on live. Any answers or suggestions would be greatly appreciated.
You have used relative paths in your scripts. When calling from Ajax, make sure the paths are getting translated to correct ones.
You can check console to check Ajax call is working correctly and then server error log to verify there are no php errors.