Reputation:
I've tried to fix this problem, or find a similar problem, but I'm still scratching my head over this one.
I have an HTML form that I'm validating with a jQuery function, then passing it to a PHP script for mailing. The problem I'm having is once the functions run, there's a duplicate mail being sent, but the second one is blank, none of the data values are passed.
The jQuery:
$("#submit").click(function(){
var quit = false;
if(validateName()){
name = validateName();
$("label#name_error").fadeOut(0);
$("label#name_error2").fadeOut(0);
} else if (validateInput('name')){
$("label#name_error").fadeOut(0);
$("label#name_error2").fadeIn(250);
quit = true;
} else {
$("label#name_error").fadeIn(250);
$("label#name_error2").fadeOut(0);
quit = true;
}
// several more validation checks for the other fields follow.
if(quit){
return false;
}
var dataString = "name=" + name + "&email=" + email; //and other fields inserted here
$.ajax({
type: "POST",
url: "bin/MailHandler.php",
data: dataString,
success: function(){
$('.error').fadeOut(0);
$('#contact-form').clearForm();
$('#contact').html("<div class='download-box'><h2>Thanks for contacting us!</h2><p>Someone will be in touch shortly!</p></div>").fadeOut(0).fadeIn(1500);
}
});
return false;
});
Which then sends to the PHP (MailHandler.php):
<?php
$to = "[email protected]";
$from = $_REQUEST["email"];
$subject = "Testing the form " . $_REQUEST["name"];
$headers = "From: " . $_REQUEST["email"] . "\r\n" . "Reply-To: " . $_REQUEST["email"];
$messageBody = "";
$messageBody .= $_REQUEST["name"] . "\n";
$messageBody .= $_REQUEST["email"] . "\n";
//and the other fields added similarly.
if (mail($to, $subject, $messageBody, $headers)){
echo ("Mail Sent");
} else {
echo ("Mail Failed");
}
?>
Once this runs, everything seems to process correctly, I get the success message and the email with all the correct data arrives.
A few minutes later a second email arrives, but with none of the data from the form. All the headers and data values are blank.
I've looked over the code, and I can't figure out where the second message is coming from. Does anyone see something I missed?? Any help is appreciated.
Thanks!
Upvotes: 0
Views: 2640
Reputation:
Turns out it was something happening server-side, rather than my code. I moved the code to a different server and experienced no problem whatsoever.
Upvotes: 0
Reputation: 4098
Maybe you will have more luck if you use the jquery .submit method rather just a click event, shown here : http://api.jquery.com/submit/
So in the case of your code:
//where #target is your form ID
$('#target').submit(function() {
//your code
return false;
});
$('#submit').click(function() {
$('#target').submit();
});
I think that will make it send the email once.
Upvotes: 0
Reputation: 17725
$('#submit').click(function(event) {
event.preventDefault();
// rest of your code
});
$_REQUEST
, use $_POST
instead.$_POST
variables are not empty before sending the email.Upvotes: 0
Reputation: 17451
It appears the form is being submitted twice. Once by the form post, then once by the AJAX call. You might want to stick with the ajax call and not use a form tag, then also use a button other than a submit
button to invoke the ajax call.
Upvotes: 1
Reputation: 1281
In your MailHandler, you should check to see if the variables you need, aren't empty, before sending an e-mail.
If the vars are empty (http://php.net/empty) then you shouldn't send the mail. It is possible that an antivirus (such as Trend Micro) check the page to make sure there aren't any viruses on it.
Upvotes: 0