Hi there I’m trying to create a simple enough php email form without any need for page refreshing using the jQuery .ajax() method, however I want all the fields to reset once the email has been sent and at the moment this isn’t happening for some reason!
Here’s my jQuery:
$.ajax({
url:"http://www.mysite.co.uk/contactMe.php",
data: {'email': email,'content': content},
success: function(response){
$("#mailContent").css({ 'box-shadow': ''});
$("#email").css({'box-shadow': ''});
$("#email").val('');
$("#mailContent").val('');
alert("DONE");
return false;
}
})
and here’s my php
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
$email = $_GET['email'];
$content = $_GET['content'];
$mailcheck = spamcheck($email);
if ($mailcheck==FALSE)
{
}
else
{//send email
mail("myemail@googlemail.com", "Subject: Website Contact Form",
$content, "From: $email" );
}
?>
Please note I’ve changed the url address and the email address for privacy, the email is sending fine as I receive it in my inbox. I’ve also tried the same thing using:
.ajax().done(function{}) and there’s no luck with that either, The worst thing is this works fine on another website I’ve used it on when I use .ajax() to create a commenting system, but it refuses to work at all on this site!!! :/
Your’s frustratedly, Simon.
EDIT: Form:
<form name="contact"> Email: <input name="email" type="text" id="email" ><br>
Message:<br> <textarea name="message" rows="20" cols="246" id="mailContent"></textarea><br>
<input class="button" value=""> </form>
You cannot use ajax to
POSTcross-domain (read up on CORS for specifics); for security reasons the browser blocks that as it might take you (and your data) to shady sites without you noticing it.If the script you are posting is on the same server, you should remove the domain name and use a path relative to the root of the web-site: