Ok this is going to be a little long winded and i'm not sure if it should be in javascript or php, I have a contact form as follows:
<form action="contact.php" method="POST" id='contact_form'>
<p><label for="name">Name</label>
<input type='text' name='name' id='name' /></p>
<div class=开发者_JS百科"clear"></div>
<p id='name_error' class='error'>Enter your name</p>
<p><label for="email">Email</label>
<input type='text' name='email' id='email' /></p>
<div class="clear"></div>
<p id='email_error' class='error'>Enter a valid email address</p>
<p><label for="telephone">Telephone</label>
<input type='text' name='telephone' id='telephone' /></p>
<div class="clear"></div>
<p id='subject_error' class='error'>Enter your telephone number</p>
<p><label for="message">Details</label>
<textarea name='message' id='message' rows="30" cols="30"></textarea></p>
<div class="clear"></div>
<p id='message_error' class='error'>Enter your message</p>
<p id='mail_success' class='success'>Thank you. We will be in contact soon.</p>
<p id='mail_fail' class='error'>Sorry, an error has occured. Please try again later.</p>
<div id="button">
<input type='submit' id='send_message' class="button" value='Submit' />
</div>
</form>
The Javascript:
$(document).ready(function(){
$('#send_message').click(function(e){
//stop the form from being submitted
e.preventDefault();
/* declare the variables, var error is the variable that we use on the end
to determine if there was an error or not */
var error = false;
var name = $('#name').val();
var email = $('#email').val();
var telephone = $('#telephone').val();
var message = $('#message').val();
if(name.length == 0){
var error = true;
$('#name_error').fadeIn(500);
}else{
$('#name_error').fadeOut(500);
}
if(email.length == 0 || email.indexOf('@') == '-1'){
var error = true;
$('#email_error').fadeIn(500);
}else{
$('#email_error').fadeOut(500);
}
if(telephone.length == 0){
var error = true;
$('#subject_error').fadeIn(500);
}else{
$('#subject_error').fadeOut(500);
}
if(message.length == 0){
var error = true;
$('#message_error').fadeIn(500);
}else{
$('#message_error').fadeOut(500);
}
//now when the validation is done we check if the error variable is false (no errors)
if(error == false){
//disable the submit button to avoid spamming
//and change the button text to Sending...
$('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
//we submit it to contact.asp
$.post("contact.php", $("#contact_form").serialize(),function(result){
//and after the ajax request ends we check the text returned
if($.trim(result) == 'sent'){
$('#button').remove();
$('#mail_fail').fadeOut(500);
$('#mail_success').fadeIn(500);
}else{
//show the mail failed div
$('#mail_fail').fadeIn(500);
//reenable the submit button by removing attribute disabled and change the text back to Send The Message
$('#send_message').removeAttr('disabled').attr('value', 'Submit');
}
});
}
});
});
and the PHP:
<?php
$EmailFrom = Trim(stripslashes($_POST['email']));
$EmailTo = "joe@bloggs.com";
$Subject = "Contact Form Message";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$telephone = Trim(stripslashes($_POST['telephone']));
$message = Trim(stripslashes($_POST['message']));
$validationOK=true;
if (Trim($name)=="") $validationOK=false;
if (Trim($email)=="") $validationOK=false;
if (Trim($telephone)=="") $validationOK=false;
if (Trim($message)=="") $validationOK=false;
if (!$validationOK) {
print "failed";
exit;
}
$Body = "";
$Body .= "name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $telephone;
$Body .= "\n";
$Body .= "message: ";
$Body .= $message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
?>
When filled out i receive the "Sorry, an error has occured. Please try again later." message but don't see where i have gone wrong?
Thanks in advance for any help.
If the result contains the whole html code of your page it means that your script does not stop at the right point or maybe the js post to a wrong url (yes i see that you specified contact.php but if there are rewrite rules in htaccess it's possible).
Try to add exit(0); at the bottom of this script. Under the last if. It seems this php is included in an other php file which displays the rest of the site after sending the e-mail.
The php code is not returning anything back to the user.
- Is your email module on in PHP?
- You should echo your message body back for debugging.
I personally avoid using print as it is just a construct and has different behaviour when you "print" multiple items back to back, it doesn't matter
Are you working on a localhost or is your code on a shared-hosting server? (ie. GoDaddy or a similar company)
If you're on your localhost you may not have a properly configured mail server. If your code is hosted do you have an email account set up with the provider from which to send your email? Does your account package offer access to the mail() function?
If you have access to the PHP error log or Apache log you should check those. You should also set up a phpinfo page so you can see how your environment is configured.
In either scenario you can certainly inspect the POST request in your browser to see if the server is returning an HTTP error. Use Firefox with Firebug or Chrome with dev tools enabled.
Is it uploaded to a server that supports PHP, because PHP wont function properly without a supporting server.
精彩评论