Skip to content

Instantly share code, notes, and snippets.

@nwittwer
Last active August 29, 2015 14:09
Show Gist options
  • Save nwittwer/30e62daa0a8f7b51c2ad to your computer and use it in GitHub Desktop.
Save nwittwer/30e62daa0a8f7b51c2ad to your computer and use it in GitHub Desktop.
A simple contact form with basic jQuery validation and AJAX form submission
<script>
// Optional jQuery Validation
function sendEmail(){
var email = document.getElementById('email').value;
var name = document.getElementById('name').value;
var message = document.getElementById('message').value;
if (name == "" )
{
alert("Please enter your full name")
document.myForm.name.focus() ;
return false;
}
if (email == "" )
{
alert("Please enter your email")
document.myForm.email.focus() ;
return false;
}
if (message == "" )
{
alert("Please enter your message")
document.myForm.message.focus() ;
return false;
}
// REQUIRED to send the data over to PHPMailer
$.ajax({
url: 'phpmailer.php', // for WordPress, use "../wp-content/themes/THEMENAME/phpmailer/phpmailer.php"
type: 'post',
data: {"email":email,"name":name,"message":message},
success: function(response) {
document.getElementById('email').value = "";
document.getElementById('name').value = "";
document.getElementById('message').value = "";
$('input[type=submit]', 'form').attr('disabled', 'disabled');
$("#myForm").addClass("message-sent");
$("#success").show();
$("#myForm").fadeOut('medium');
} //finish ajax
});
} // end sendEmail function
</script>
<form name="myForm" id="myForm" action="javascript:sendEmail();">
<label>Your Name</label>
<input type="name" id="name" />
<label>Your Email</label>
<input type="email" id="email" />
<label>How can we help you? Please explain.</label>
<textarea type="text" id="message" cols="10" rows="7"></textarea>
<input type="submit"></input>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment