Skip to content

Instantly share code, notes, and snippets.

@colllin
Created April 29, 2013 02:39
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 colllin/5479411 to your computer and use it in GitHub Desktop.
Save colllin/5479411 to your computer and use it in GitHub Desktop.
<?php
// only run this code if they submitted the form (we can check if the submit button was pressed)
if (isset($_POST['contact-submit'])) {
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
success = true;
// validate email
if ($email == '') {
// TODO: handle 'missing email' error
success = false;
} elseif (!preg_match('/^[^@]+@[^@]+$/', $email)) {
// TODO: handle 'this doesnt look like an email' error
success = false;
}
// do similar for name and message
if (success) {
mail('info@taxifriend.com', 'Incoming Transmission', "From $name, $email, $phone:\r\n----------------------\r\n$message");
?>
<p><strong>Thank you!</strong> Your message has been sent.</p>
<?php
// redirect to self so that pressing "refresh" doesn't re-submit form data
$SELFPAGE = $_SERVER['PHP_SELF'];
header("location: $SELFPAGE");
exit;
} else {
?>
<p><strong>Uh oh.</strong> Please make sure all fields are filled out and that the information is correct.</p>
<?php
}
}
?>
<form method="POST" action="">
<div>
<label for="contact-email">Email</label>
<input id="contact-email" type="email" name="email" value="<?php echo $email; ?>" placeholder="you@example.com">
</div>
<div>
<label for="contact-name">Name</label>
<input id="contact-name" type="text" name="name" value="<?php echo $name; ?>" placeholder="John Smith">
</div>
<div>
<label for="contact-message">Message</label>
<textarea id="contact-message" name="message" placeholder="Type a message... (optional)"><?php echo $message; ?></textarea>
</div>
<div>
<input type="submit" name="contact-submit" value="Please Contact Me">
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment