Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created February 28, 2011 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloberg/847667 to your computer and use it in GitHub Desktop.
Save mloberg/847667 to your computer and use it in GitHub Desktop.
Contact Form
<?php
// get a recaptcha api key at http://www.google.com/recaptcha/whyrecaptcha
require_once("path/to/recaptchalib.php");
$publickey = "";
$privatekey = "";
// see if the form has been submitted
if($_POST['submit']){
// error checking
$errors = '';
$resp = recaptcha_check_answer($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
if(!preg_match('/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/', $_POST["email"])){
$errors .= "<p>Email Address is not valid.</p>";
}
if(!$resp->is_valid){
$errors .= "<p>reCAPTCHA did not match.</p>";
}
if($errors === ''){
// you can set different inquiry types to go to different emails
$to = array(
"general" => "mail@example.com",
"option2" => "mail@example.com"
);
// you can set the email subject based on the inquiry type
$subject_array = array(
"general" => "Web Contact Form",
"option2" => "Custom Subject"
);
$subject = $subject_array[$_POST['to']]; // or you could set a static subject
$address = $to[$_POST['to']]; // or you could set a static email
$message = $_POST['message'];
mail($address, $subject, $message, "From: {$_POST['email']}");
include('sent.php');
}else{
include("contact_form.php");
}
}else{
include('contact_form.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Contact Form</title>
<link rel="stylesheet" href="form.css" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script>
// recaptcha styling - http://wiki.recaptcha.net/index.php/Theme
var RecaptchaOptions = {
theme: 'white'
};
</script>
</head>
<body>
<div id="wrapper">
<form action="" method="post">
<label for="to">Inquiry Type:</label>
<select name="to">
<option value="general" selected="selected">General</option>
<option value="option2">Option 2</option>
</select>
<br />
<label for="email">Email:</label>
<input name="email" value="<?php if($_POST['email']) echo $_POST['email'];?>" /><br />
<label for="message">Message:</label>
<textarea name="message"><?php if($_POST['message']) echo $_POST['message'];?></textarea><br />
<div id="recaptcha">
<?php echo recaptcha_get_html($publickey);?>
</div>
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
<div id="errors"><?php if($errors) echo $errors;?></div>
</div>
</body>
</html>
label,input,select,textarea,#recaptcha,#errors,#errors p{
margin: 0;
padding: 0;
}
label{
float: left;
width: 150px;
font-family: "Helvetica", sans-serif;
font-size: 18px;
padding-top: 5px;
}
textarea,input,select{
margin-bottom: 10px;
width: 302px;
padding: 5px;
color: #666;
background: #f5f5f5;
border: 1px solid #ccc;
font: 1.5em "Lucida Grande", "Lucida Sans Unicode", Arial, sans-serif;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
input:focus,select:focus,textarea:focus{
border: 1px solid #999;
background-color: #fff;
color:#333;
}
#recaptcha,input[type=submit],#errors{
margin-left: 150px;
margin-bottom: 10px;
}
select{
color: #000;
width: 312px;
}
textarea{
height: 150px;
}
input[type=submit]{
cursor: pointer;
border: 1px solid #222;
background: #333;
color: #fff;
width: 100px;
}
input[type=submit]:hover{background:#444;}
#errors p{
color: red;
font-weight: bold;
font-family: "Helvetica", sans-serif;
font-size: 14px;
}
#recaptcha_table{
color: #ccc;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Message Sent</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<p>Message sent!</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment