Skip to content

Instantly share code, notes, and snippets.

@jeffreysbrother
Last active October 14, 2015 04:16
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 jeffreysbrother/004ca4524ba586b5c07f to your computer and use it in GitHub Desktop.
Save jeffreysbrother/004ca4524ba586b5c07f to your computer and use it in GitHub Desktop.
Contact page with validation. This page also assumes the use of PHPMailer, a config file, and header/footer includes. In order to use, we must update the $address variable as well as other values in that section. This implementation also assumes that the site will have clean URLs with subfolders. Also, it should be obvious that many of the class…
<?php
require_once("../inc/config.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$phone = trim($_POST["phone"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == ""){
$error_message = "You must first specify a value for the NAME, EMAIL, and MESSAGE fields.";
}
if (isset($error_message)) {
foreach( $_POST as $value ){
if(stripos($value, 'Content-Type:') !== FALSE ){
$error_message = "There was a problem with the information submitted.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once(ROOT_PATH . "inc/class.phpmailer.php");
$mail = new PHPMailer();
if (!isset($error_message) && (!$mail->ValidateAddress($email) OR stripos($email, '.com') == FALSE)){
$error_message = "Please specify a valid email address.";
}
if (!isset($error_message)) {
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom($email, $name);
$address = "joe@blahblahblah999.com";
$mail->AddAddress($address, "Joe Blah");
$mail->Subject = "Joe Blah Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if($mail->Send()){
header("Location: " . BASE_URL . "contact/?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
?>
<?php
$pageTitle = "contact.php";
include (ROOT_PATH . "inc/head.php");
?>
<!-- <div id="top_spacer"></div> -->
<div id="title">
<h2 class="first">CONTACT</h2>
</div>
<?php
if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<h2 class="thanks">Thanks for the email!</h2>
<?php } else { ?>
<?php
if (isset($error_message)) {
echo '<p class="message">' . $error_message . '</p>';
}
?>
<div class="bottom_spacer"></div>
<div class="content1">
<form class="contact_form" method="post" action="<?php echo BASE_URL; ?>contact/">
<p>
<input class="form1" type="text" name="name" id="name" placeholder="name" value="<?php if (isset($name)) { echo htmlspecialchars($name); }?>">
</p>
<p>
<input class="form1" type="text" name="email" id="email" placeholder="email" value="<?php if (isset($email)) { echo htmlspecialchars($email); }?>">
</p>
<p>
<textarea name="message" id="message" placeholder="message"><?php if (isset($message)) { echo htmlspecialchars($message); }?></textarea>
</p>
<p style="display: none;">
<input type="text" name="address" id="address">
<p style="display: none;">Humans: please leave this field blank.</p>
</p>
<input class="button1" type="submit" value="SUBMIT">
</form>
</div>
<?php } ?>
<?php
include (ROOT_PATH . 'inc/footer.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment