Created
May 14, 2013 07:43
-
-
Save melissacabral/5574327 to your computer and use it in GitHub Desktop.
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 | |
// A helper function to make generating the HTML for an error message easier | |
function sticky_field($field){ | |
if(isset($field)){ | |
echo $field; | |
} | |
} | |
function print_error($key, $errors) { | |
if (isset($errors[$key])) { | |
$message = $errors[$key]; | |
echo "<div class='error message'>$message</div>"; | |
} | |
} | |
function hilight_field($key, $errors){ | |
if (isset($errors[$key])) { | |
echo 'class="error"'; | |
} | |
} | |
function checked( $val1, $val2 ){ | |
if($val1 == $val2){ | |
echo 'checked="checked"'; | |
} | |
} | |
//parse the form if it was submitted | |
if( $_POST['did_submit'] ): | |
//extract and sanitize user submitted data | |
$name = filter_var( trim($_POST['name']), FILTER_SANITIZE_STRING ); | |
$email = strip_tags(trim($_POST['email'])); | |
$phone = filter_var( trim($_POST['phone']), FILTER_SANITIZE_NUMBER_INT); | |
$message = filter_var( trim($_POST['message']), FILTER_SANITIZE_STRING ); | |
$newsletter = $_POST['newsletter']; | |
//todo: validate! | |
//check length of name * required field | |
$valid = true; | |
if(strlen($name) < 1){ | |
$valid = false; | |
$errors['name'] = 'name is empty'; | |
} | |
if(strlen($message) < 1){ | |
$valid = false; | |
$errors['message'] = 'message is empty'; | |
} | |
//check email pattern | |
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){ | |
$valid = false; | |
$errors['email'] = 'bad email'; | |
} | |
//clean newsletter value | |
if( 1 == $newsletter ): | |
$newsletter = 'YES!'; | |
else: | |
$newsletter = 'NO!'; | |
endif; | |
//if validation passed, send the mail! | |
if( true == $valid): | |
//get ready to send mail - set up mail() parameters | |
$to = '[email protected], [email protected]'; | |
$subject = 'Contact form from wp310 class demo'; | |
$body = "Name: $name \n"; | |
$body .= "Email: $email \n"; | |
$body .= "Phone: $phone \n\n"; | |
$body .= "Add to Newsletter List? $newsletter \n\n"; | |
$body .= "Message: $message \n\n"; | |
$header = "Reply-to: $email \r\n"; | |
$header .= "From: $name \r\n"; | |
//send it! did_send will equal 1 if the mail sends, 0 if it fails to send | |
$did_send = mail( $to, $subject, $body, $header ); | |
//handle success/failure user feedback | |
if( $did_send ): | |
$display_msg = 'Thank you for your message, ' . $name . ', I will get back to you soon.'; | |
$css_class = 'success'; | |
else: | |
$display_msg = 'Sorry, there was a problem sending your message.'; | |
$css_class = 'error'; | |
endif; //did_send | |
endif; //valid | |
endif; //did_submit | |
?> | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Example Contact Form - Simple</title> | |
<style> | |
input[type=text], | |
input[type=submit], | |
textarea{ | |
display: block; | |
} | |
.success{ | |
color:green; | |
} | |
.error{ | |
color:red; | |
background-color: #FDD7DB; | |
} | |
</style> | |
</head> | |
<body> | |
<header> | |
<h1>Contact Me!</h1> | |
</header> | |
<?php | |
if( isset($msg) ): | |
echo "<div class=\"$css_class\">"; | |
echo $msg; | |
echo '</div>'; | |
endif; | |
?> | |
<?php | |
//hide the form if it sent successfully | |
if( !$did_send ): ?> | |
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> | |
<label for="name">Your Name:</label> | |
<input type="text" name="name" id="name" <?php hilight_field('name', $errors) ?> value="<?php sticky_field($name); ?>" /> | |
<?php print_error('name', $errors); ?> | |
<label for="email">Your Email Address:</label> | |
<input type="text" name="email" id="email" <?php hilight_field('email', $errors) ?> value="<?php sticky_field($email); ?>" /> | |
<?php print_error('email', $errors); ?> | |
<label for="phone">Your Phone Number:</label> | |
<input type="text" name="phone" id="phone" value="<?php sticky_field($phone); ?>" /> | |
<?php print_error('phone', $errors); ?> | |
<label for="message">Your Message:</label> | |
<textarea name="message" id="message" <?php hilight_field('message', $errors) ?>><?php sticky_field($message); ?></textarea> | |
<?php print_error('message', $errors); ?> | |
<input type="checkbox" name="newsletter" value="1" id="newsletter" <?php checked('YES!', $newsletter); ?> /> | |
<label for="newsletter">I would like to receive the newsletter!</label> | |
<input type="submit" value="Send Message" /> | |
<input type="hidden" name="did_submit" value="1" /> | |
</form> | |
<?php endif; //hide form if did_send ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment