I'm a WordPress designer, I developed a contact form for one of my themes that's validated via jQuery.
Please check the code below, then read the notes beneath.
$('.submitemail') .click(function() {
//VALIDATION CODE GOES HERE
if ( /*VALIDATED SUCCESSFULLY*/ ) {
$.ajax({
type: 'POST',
url: templatePath+'/lib/scripts/sendEmail.php',
data: 'visitorname=' + visitorname + '&visitoremail=' + visitoremail + '&visitormessage=' + visitormessage,
success: function(contactResults) {
//SUCCESS CODE
}
});
}
});
Notes:
- sendEmail.php is a correct script that sends email using PHPmailer class.
- templatePath variable has the value of the full template path which looks like this: http://somedomain.com/wp-content/themes/themename
- The jQuery code above is located in lib/scripts/jfunctions.js (same directory of the php script)
- The whole process (ajax and php) works perfectly as expected in many servers, (tested in two servers by me and other servers by my theme users).
The Problem:
In SOME servers, the success handler is not triggered while the ajax call to sendEmail.php is actually passed successfully and the php script is processed and email is sent.
When I check with firebug to see why the success handler is not triggered, firebug shows "not found 404 error", It's like a false alarm.
Possible causes:
I think some servers is configured to block such ajax calls.
What might be the cause for this weird issue? How to fix it?
Thanks in advance.
@nowk: sendEmail.php code is:
<?php
// Code for loading WordPress environment goes here //
$themeName_optionTree = get_option('option_tree');
$name = trim($_POST['visitorname']);
$email = $_POST['visitoremail'];
$message = $_POST['visitormessage'];
$site_owners_email = $themeName_optionTree['owner_email'];
$site_owners_name = $themeName_optionTree['owner_name'];
$email_subject = $themeName_optionTree['email_subject'];
$success_message = '<p class="success-box">' . $themeName_optionTree['success_message'] . '</p>';
if (strlen($name) < 2) {
$error['name'] = 1;
}
if (!preg_match('/^[a-z0-9&'.-_+]+@[a-z0-9-]+.([a-z0-9-]+.)*+[a-z]{2}/is', $email)) {
$error['email'] = 1;
}
if (strlen($message) < 2) {
$error['message'] = 1;
}
if (!$error) {
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer(true);
try {
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = $email_subject;
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $message;
$mail->Send();
echo $success_message;
} catch (phpmailerException $e) {
echo '<p class="warning-box">' . $e->errorMessage() . '</p>';
} catch (Exception $e) {
echo '<p class="warning-box">' . $e->getMessage() . '</p>';
}
}
?>
Please note that the above code executes perfectly even when ajax returns 404, weird huh!.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…