Last active
November 10, 2015 15:41
-
-
Save jongrover/10486208 to your computer and use it in GitHub Desktop.
Front-end / Back-end Validated Email Mailer Form Submission Solution using HTML5, PHP5, JavaScript, and jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input.error, textarea.error { | |
border: 1px solid red; | |
} | |
span.error { | |
display: none; | |
color: red; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
////////// Contact Form Validation ////////// | |
function validateName(fullname) { | |
if(fullname.length > 2) { | |
$('#fullname').removeClass('error'); | |
$('#fullname-error').hide(); | |
return true; | |
} | |
else { | |
$('#fullname').addClass('error'); | |
$('#fullname-error').show(); | |
return false; | |
} | |
} | |
function validateEmail(email) { | |
var re = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/; | |
if (re.test(email)) { | |
$('#email').removeClass('error'); | |
$('#email-error').hide(); | |
return true; | |
} | |
else { | |
$('#email').addClass('error'); | |
$('#email-error').show(); | |
return false; | |
} | |
} | |
function validateMessage(message) { | |
if (message.length > 0) { | |
$('#message').removeClass('error'); | |
$('#message-error').hide(); | |
return true; | |
} | |
else { | |
$('#message').addClass('error'); | |
$('#message-error').show(); | |
return false; | |
} | |
} | |
$('form').submit(function(event){ | |
var fullname = $('#fullname').val(), | |
email = $('#email').val(), | |
message = $.trim($('#message').val()); | |
if (validateName(fullname) & validateEmail(email) & validateMessage(message)) { | |
return true; //submit form. | |
} | |
else { | |
event.preventDefault(); //prevent form from submitting! | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ($_GET['s'] == 'success') { ?> | |
<p>Thank you. Your message has been sent successfully!</p> | |
<?php } else if ($_GET['s'] == 'error') { ?> | |
<p>Error. Your message was not sent! Make sure you type in the proper verification numbers. <a href="contact.php">Click here to try again</a>. If issues persist please send an email to site admin (site admin email here).</p> | |
<?php } else { ?> | |
<!-- Contact Form --> | |
<form action="mailer.php" method="post"> | |
<label for="fullname">Name</label> | |
<span id="fullname-error" class="error">must be more than two characters.</span> | |
<input type="text" id="fullname" name="fullname" placeholder="your name"> | |
<label for="email">Email</label> | |
<span id="email-error" class="error">must be a valid email.</span> | |
<input type="text" id="email" name="email" placeholder="your email"> | |
<label for="phone">Phone</label> | |
<input type="tel" id="phone" name="phone" placeholder="your phone (optional)"> | |
<label for="message">Message</label> | |
<span id="message-error" class="error">can not be left blank.</span> | |
<textarea id="message" name="message" rows="8"></textarea> | |
<input type="text" name="verify" class="verify-box" placeholder="verify you're human"> | |
<img class="verify-img" src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image"> | |
<input type="submit" value="send"> | |
</form> | |
<?php } ?> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> | |
<script src="contact.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
################################### | |
# Jonathan Grover - Simple Mailer # | |
# v0.2 Beta for PHP 5 # | |
# Date Modified: 4/11/2014 # | |
# MIT License (see details below) # | |
################################### | |
// Set email address to send to i.e: you@yoursite.com | |
$sendTo = 'you@yoursite.com'; | |
// Set email subject (appears as subject) | |
$subject = 'Exceptional Realty - Contact Form Submission'; | |
//Set link to your success page | |
$success_page = 'contact.php?s=success'; | |
//Set link to your error page | |
$error_page = 'contact.php?s=error'; | |
########## No need to touch the code below this line! ########## | |
// get posted form values | |
$name = $_POST['fullname']; | |
$email = $_POST['email']; | |
$phone = $_POST['phone']; | |
$message = $_POST['message']; | |
$verify = $_POST['verify']; | |
// setup email header | |
$header = "From: ".$email; | |
// construct email body | |
$body = "Name: ".$name."\r\n". | |
"Email: ".$email."\r\n". | |
"Phone: ".$phone."\r\n". | |
"\r\n". | |
"Message: ".$message; | |
// make sure verification is correct | |
if(md5($verify).'a4xn' == $_COOKIE['tntcon']) { | |
// send email | |
mail($sendTo, $subject, $body, $header); | |
//set verify cookie | |
setcookie('tntcon',''); | |
//go to success page | |
header("Location: $success_page"); | |
exit; | |
} | |
else { | |
//go to error page | |
header("Location: $error_page"); | |
exit; | |
} | |
/* | |
Copyright (c) 2013 Jonathan Grover | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Verfication Image Generator - No need to touch this code! | |
* Available under an MIT License (see details below) | |
*/ | |
header('Content-type: image/jpeg'); | |
$width = 50; | |
$height = 30; | |
$my_image = imagecreatetruecolor($width, $height); | |
imagefill($my_image, 0, 0, 0xFFFFFF); | |
// add noise | |
for ($c = 0; $c < 40; $c++) { | |
$x = rand(0,$width-1); | |
$y = rand(0,$height-1); | |
imagesetpixel($my_image, $x, $y, 0x000000); | |
} | |
$x = rand(1,10); | |
$y = rand(1,10); | |
$rand_string = rand(1000,9999); | |
imagestring($my_image, 5, $x, $y, $rand_string, 0x000000); | |
setcookie('tntcon',(md5($rand_string).'a4xn')); | |
imagejpeg($my_image); | |
imagedestroy($my_image); | |
/* | |
Copyright (c) 2013 Jonathan Grover | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment