I'm attempting to submit a contact form to a php file via ajax. I use phpMailer to send form results. The POST doesnt appear to be working however. When I submit, neither the success or error conditions execute. When I comment out the POST variables, the ajax request executes successfully and the email sends (variables blank). Here is the code...
<form method="post" class="form2" id="form2Id" name="form" action="">
<div class="formtitle">Form<</div>
<div class="input">
<div class="inputtext">Your Name: </div>
<div class="inputcontent">
<input id="nameId" type="text" name="name" />
</div>
</div>
<div class="input">
<div class="inputtext">Your Phone Number: </div>
<div class="inputcontent">
<input id="phoneNumberId" type="text" name="phoneNumber" />
</div>
</div>
<div class="input">
<div class="inputtext">Your Email Address: </div>
<div class="inputcontent">
<input id="emailAddressId" type="text" name="emailAddress"/>
</div>
</div>
<div class="inputtextbox nobottomborder">
<!--<div class="inputtext">Message: </div>-->
<div class="inputcontent">
<!--<textarea id="messageId" class="textarea" name="message"></textarea>-->
</div>
</div>
<div class="buttons">
<input class="orangebutton" type="submit" id="formSubmit" value="Submit" />
</div>
</form>
$( "#formSubmit" ).click(function() {
$("#form2Id").validate({
debug: true,
submitHandler: function(form) {
$.ajax({
type: "POST",
url: "handler.php",
data: $(form).serialize(),
timeout: 12000,
success: function() { alert("Success"); },
error: function() { alert("FAILURE"); }
});
return false;
}
});
});
)};
<?php
require("./PHPMailer-master/class.phpmailer.php");
include("./PHPMailer-master/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
//collect the posted variables into local variables before calling $mail = new mailer
$nameId = $_POST['nameId'];
$phoneNumberId = $_POST['phoneNumberId'];
$emailAddressId = $_POST['emailAddressId'];
$mail = new PHPMailer();
//$body = file_get_contents('contents.html');
//$body = eregi_replace("[]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "lll"; // SMTP server
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "lll@gmail.com"; // GMAIL username
$mail->Password = "..."; // GMAIL password
$mail->SetFrom('lll@gmail.com', 'UUU');
$mail->AddReplyTo("lll@gmail.com","UUU");
$mail->Subject = "Email subject";
optional, comment out and test
$mail->MsgHTML($body);
$address = $emailAddressId;
$mail->AddAddress($address, "MMM");
$mail->Body="
Name: $nameId
Phone: $phoneNumberId
Email: $emailAddressId
";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "
<h2>Thank you. We will contact you shortly.</h2>
";
}
?>
Any help would be greatly appreciated.
Given html line <input id="nameId" type="text" name="name" />
and this php code $nameId = $_POST['nameId'];
you will indeed end up with an empty $nameId
, because the posted value is in $_POST['name']
. The same goes for your other variables.
If I comment out the post vars,
$nameId = $_POST['name'];
$phoneNumberId = $_POST['phoneNumber'];
$emailAddressId = $_POST['emailAddress'];
the email sends successfully as long as I type in a valid email address. Otherwise, the email does not send at all. In both instances, ajax returns a successful response. I've tried the above suggestion of using the names instead of the ids, but it sill does not work.