Skip to content

Instantly share code, notes, and snippets.

@seafarer
Forked from ajtroxell/contact.html
Created March 17, 2014 02:19
Show Gist options
  • Save seafarer/9592853 to your computer and use it in GitHub Desktop.
Save seafarer/9592853 to your computer and use it in GitHub Desktop.
<form id="contact" name="contact" method="post">
<fieldset>
<label for="name" id="name">Name<span class="required">*</span></label>
<input type="text" name="name" id="name" size="30" value="" required/>
<label for="email" id="email">Email<span class="required">*</span></label>
<input type="text" name="email" id="email" size="30" value="" required/>
<label for="phone" id="phone">Phone</label>
<input type="text" name="phone" id="phone" size="30" value="" />
<label for="Message" id="message">Message<span class="required">*</span></label>
<textarea name="message" id="message" required></textarea>
<label for="Captcha" id="captcha">Name the small house pet that says "<i>meow</i>"<span class="required">*</span></label>
<input type="text" name="captcha" value="" required/>
<input id="submit" type="submit" name="submit" value="Send" />
</fieldset>
</form>
<div id="success">
<span class="green textcenter">
<p>Your message was sent succssfully! I will be in touch as soon as I can.</p>
</span>
</div>
jQuery.validator.addMethod('answercheck', function (value, element) {
return this.optional(element) || /^\bcat\b$/.test(value);
}, "type the correct answer -_-");
<?php
$to = "[email protected]";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "You have a message sent from your site";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$send = mail($to, $subject, $body, $headers);
?>
// validate contact form
$(function() {
$('#contact').validate({
rules: {
name: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
message: {
required: true
},
captcha: {
required: true,
answercheck: true
}
},
messages: {
name: {
required: "come on, you have a name don't you?",
minlength: "your name must consist of at least 2 characters"
},
email: {
required: "no email, no message"
},
message: {
required: "um...yea, you have to write something to send this form.",
minlength: "thats all? really?"
},
captcha: {
required: "sorry, wrong answer!"
}
},
submitHandler: function(form) {
$(form).ajaxSubmit({
type:"POST",
data: $(form).serialize(),
url:"process.php",
success: function() {
$('#contact').fadeTo( "slow", 0.15, function() {
$('#success').fadeIn();
});
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment