I'm sure this is a problem that's been answered many times, but I am still having problems with my contact form after spending hours trying to figure out the problem.
When I submit the form, I get re-directed to the submit.php page with the Thank you message, but no email is sent to my $email_to address.
It's probably worth noting that this code is inside a Wordpress theme and it is currently running on my local machine (will this affect anything though?)
Can anyone tell me if there is anything I'm doing wrong here? I have a feeling I'm using the plugins incorrectly, quite new to this ajax game!
I'm using the following plugins:
- Jquery Form Plugin (http://pastebin.com/cC5899ns)
- Jquery Validation (http://pastebin.com/GyYuVwAH)
Here's my HTML:
<div id="footer">
<h3>Contact</h3>
<div id="preview"></div>
<form name="form" id="form" action="<?php bloginfo('template_directory'); ?>/_/inc/submit.php" method="post">
<input type="text" value="Your name" name="name" />
<input type="tel" value="Your contact number" name="email" />
<textarea name="message">Quick Note</textarea>
<button>Submit</button>
</form>
</div>
My JS:
$(document).ready(function (){
$('#form').validate(
{
rules:
{
"name":{
required:true,
maxlength:40
},
"email":{
required:true,
email:true,
maxlength:100
},
"message":{
required:true
}},
messages:
{
"name":{
required:"This field is required"
},
"email":{
required:"This field is required",
email:"Please enter a valid email address"
},
"message":{
required:"This field is required"
}},
submitHandler: function(form){
$(form).ajaxSubmit({
target: '#preview',
success: function() {
alert('hello');
$('#foooter').slideUp('fast');
}
});
}
})
});
My Submit.php
<?php
include("db.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['message']);
if(strlen($name)>0 && strlen($email)>0 && strlen($message)>0)
{
$email_to = "*********";
$email_from = $email;
$email_subject = "Contact Form";
$email_message = stripslashes($message);
$headers = 'From: '.$email_from."\r\n" .
'Reply-To: '.$email_from."\r\n";
mail($email_to, $email_subject, $email_message, $headers);
$time=time();
mysql_query("INSERT INTO contact (name,email,message,created_date) VALUES('$name','$email','$message','$time')");
echo "<h1>Thank You !</h1>";
}
}
?>
My db.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "*****";
$mysql_database = "******";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
?>