Created
July 3, 2013 13:09
-
-
Save jongamble/5917723 to your computer and use it in GitHub Desktop.
This is a basic contact form to build off of.
This file contains hidden or 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 //Let's Contact Form This Thing UP! | |
// Start Your PHP Sessions | |
session_start(); | |
// Set the contact page variable for redirection later | |
$contact_page = get_permalink(48); | |
// Create the necessary variable that will be used in detecting errors | |
$errorFound = false; | |
$errorReport = ""; | |
// Create variables and retrieve the values for each element of the main form. Sanitize the data. | |
// Includes a ternary fallback for placeholder.js in IE 8, since the placeholder text goes through as the value when empty. | |
$firstName = ($_POST['firstName']=='First Name *')?'':stripslashes(trim($_POST['firstName'])); | |
$lastName = ($_POST['lastName']=='Last Name *')?'':stripslashes(trim($_POST['lastName'])); | |
$phone = ($_POST['userPhone']=='Phone Number *')?'':stripslashes(trim($_POST['userPhone'])); | |
$email = ($_POST['userEmail']=='Email Address')?'':stripslashes(trim($_POST['userEmail'])); | |
$comment = stripslashes(trim($_POST['userComments'])); | |
// check for spam in a hidden field | |
$human_test = stripslashes(trim($_POST['human_test'])); | |
// validateForm() | |
// This function verifies that the form has been filled out correctly | |
function validateForm() { | |
// Start by making the necessary variables "global" so that they can be accessed outside of this function | |
global $errorFound, $errorReport, $errorField, $firstName, $lastName, $phone, $email; | |
// Check to make sure that the necessary form elements were filled out | |
$errorField = array(); | |
if(empty($firstName)) { | |
$errorFound = true; | |
$errorReport .= "<p class=\"subtitle\">Please provide your first name.</p>\n"; | |
$errorField['firstName'] = true; | |
} | |
if(empty($lastName)) { | |
$errorFound = true; | |
$errorReport .= "<p class=\"subtitle\">Please provide your last name.</p>\n"; | |
$errorField['lastName'] = true; | |
} | |
if(empty($phone)){ | |
$errorFound = true; | |
$errorReport .= "<p class=\"subtitle\">Please provide your phone number.</p>"; | |
$errorField['phone'] = true; | |
} | |
// If any errors were found, then return false | |
if($errorFound) { | |
return false; | |
} | |
// If no errors were found, then return true | |
return true; | |
} | |
// validContent() | |
// This function filters through the content of the form and checks for possible spam issues | |
function validContent() { | |
// Start by making the necessary variables "global" so that they can be accessed outside of this function | |
global $firstName, $lastName, $phone, $email, $comment, $human_test; | |
$spamFound = false; | |
// Set up the default spam values to check against | |
$spamValues = array('http:', 'href=', 'SEO'); | |
// set up the list of user input values to check for spam | |
$checkVars = array($firstName, $lastName, $phone, $email, $comment); | |
// Loop through each input value and check it against each spam value | |
foreach($checkVars as $var){ | |
foreach($spamValues as $sv){ | |
// If there is a match, change spamFound to true | |
if(strstr($var, $sv)!=false){$spamFound = true;} | |
} | |
} | |
// Check to see if the human test field was filled out | |
if(!empty($human_test)) { $spamFound = true; } | |
// If any problems were found, then return false | |
if($spamFound) { | |
return false; | |
} | |
// Otherwise, return true | |
return true; | |
} | |
// Check to see if the form has been submitted | |
if(isset($_POST['submit'])) { | |
// If it has been submitted, begin validating the form | |
// Create variables necessary for sending the form information and fill the body of the email with the appropriate information. | |
$toAddress = ""; | |
$bccToAddress = ""; | |
if(validateForm() == false) { | |
// If the form does not pass validation, then do nothing for right now. This will be handled later on in the script. | |
} | |
else { | |
// Create the body of the email | |
$body = "\nThe information provided in this email was submitted from example.com.\n"; // Change the domain name on this line | |
$body .= "Please contact the individual below within 24 hours of receiving this message.\n\n\n"; | |
$body .= "NAME: " . $firstName . " " . $lastName . "\n"; | |
$body .= "PHONE: " . $phone . "\n"; | |
$body .= "EMAIL: " . $email . "\n"; | |
$body .= "\nCOMMENTS:\n" . $comment . "\n"; | |
$body .= "\n\n\n"; | |
$body .= "-- End of Message."; | |
// Finalize the details of the email before sending it. | |
// If the form's content passes the spam filters, then send the form as you normally would | |
if(validContent()) { | |
$toAddress = "[email protected]"; // Change the toAddress to reflect the recipient of the email | |
$bccToAddress = "[email protected]"; // Change the bccToAddress to reflect any backup email addresses. If there aren't any bcc addresses, comment out this line and the bcc declaration in the header. | |
$fromAddress = "[email protected]"; // Change the from address to reflect the email domain of the website | |
$emailSubject = "Contact Form from - example.com - " . $subject_date; // Update the domain for the email header | |
$emailHeader = "From: " . $fromAddress . "\r\n"; | |
$emailHeader .= "Reply-To: " . $fromAddress . "\r\n"; | |
// $emailHeader .= "Bcc: " . $bccToAddress . "\r\n"; // Comment this out if you are not utilizing the BCC address field | |
$emailHeader .= "X-Mailer: PHP/" . phpversion(); | |
// Finaly, email the results of the form to the appropriate person. Allow the script to continue to run at this point. | |
mail($toAddress, $emailSubject, $body, $emailHeader); | |
// Redirect the user to the thank you page if everything worked properly | |
header('Location: '.$contact_page.'?thank_you=true'); | |
} else { | |
// if an issue occured, redirect the user to an error page | |
header('Location: '.$contact_page.'?error_code=1'); | |
} | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
</head> | |
<body> | |
<?php | |
if($_GET['thank_you'] == 'true'){ | |
echo "<div class='successBox'>\n"; | |
echo "<p>Your form has been submitted successfully.</p>\n"; | |
echo "<p class='subtitle'>We will be in touch with you very shortly. If you have further questions, please call us at 855.555.5555.</p>\n"; | |
echo "<p class='subtitle'>Have a great day!</p>\n"; | |
echo "</div>\n"; | |
} else { | |
if($_GET['error_code'] == '1'){ | |
echo "<div class='errorBox'>\n"; | |
echo "<p>There is a critical errow with your form.</p>\n"; | |
echo "<p class='subtitle'>We apologize for the inconvienence. Please try resubmitting your data and verify it is correct. If you continue to see this message, please try again later.</p>\n"; | |
echo "<p class='subtitle'>Have a great day!</p>\n"; | |
echo "</div>\n"; | |
} elseif($errorFound){ | |
echo "<div class='errorBox'>\n"; | |
echo $errorReport; | |
echo "</div>\n"; | |
} else {} ?> | |
<h3 class="h3">Contact Form:</h3> | |
<form action="<?=$contact_page?>" method="post" class="contactForm"> | |
<label for="firstName" class="hiddenLabel">First Name *</label> | |
<input type="text" id="firstName" name="firstName" placeholder="First Name *" value="<?=$firstName?>" class="text-box-class<?php if($errorField['firstName']){echo ' error';}?>"> | |
<label for="lastName" class="hiddenLabel">Last Name *</label> | |
<input type="text" id="lastName" name="lastName" placeholder="Last Name *" value="<?=$lastName?>" class="text-box-class<?php if($errorField['lastName']){echo ' error';}?>"> | |
<label for="userEmail" class="hiddenLabel">Email Address</label> | |
<input type="text" id="userEmail" name="userEmail" class="text-box-class" placeholder="Email Address" value="<?=$email?>"> | |
<label for="userPhone" class="hiddenLabel">Phone Number *</label> | |
<input type="text" id="userPhone" name="userPhone" placeholder="Phone Number *" value="<?=$phone?>" class="text-box-class<?php if($errorField['phone']){echo ' error';}?>"> | |
<label for="userComments" class="hiddenLabel">Comments</label> | |
<textarea name="userComments" id="userComments" class="textarea" placeholder="Comments / Questions"><?=$comments?></textarea> | |
<input type="submit" class="submitButton button-green" name="submit" id="submit" value="Submit"> | |
</form> | |
<?php } ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment